## 條件
* 使用where子句對表中的數據篩選,結果為true的行會出現在結果集中
* 語法如下:
~~~
select * from 表名 where 條件;
~~~
## 比較運算符
* 等于=
* 大于>
* 大于等于>=
* 小于<
* 小于等于<=
* 不等于!=或<>
* 查詢編號大于3的學生
~~~
select * from students where id>3;
~~~
* 查詢編號不大于4的科目
~~~
select * from subjects where id<=4;
~~~
* 查詢姓名不是“黃蓉”的學生
~~~
select * from students where sname!='黃蓉';
~~~
* 查詢沒被刪除的學生
~~~
select * from students where isdelete=0;
~~~
## 邏輯運算符
* and
* or
* not
* 查詢編號大于3的女同學
~~~
select * from students where id>3 and gender=0;
~~~
* 查詢編號小于4或沒被刪除的學生
~~~
select * from students where id<4 or isdelete=0;
~~~
## 模糊查詢
* like
* %表示任意多個任意字符
* _表示一個任意字符
* 查詢姓黃的學生
~~~
select * from students where sname like '黃%';
~~~
* 查詢姓黃并且名字是一個字的學生
~~~
select * from students where sname like '黃_';
~~~
* 查詢姓黃或叫靖的學生
~~~
select * from students where sname like '黃%' or sname like '%靖%';
~~~
## 范圍查詢
* in表示在一個非連續的范圍內
* 查詢編號是1或3或8的學生
~~~
select * from students where id in(1,3,8);
~~~
* between ... and ...表示在一個連續的范圍內
* 查詢學生是3至8的學生
~~~
select * from students where id between 3 and 8;
~~~
* 查詢學生是3至8的男生
~~~
select * from students where id between 3 and 8 and gender=1;
~~~
## 空判斷
* 注意:null與''是不同的
* 判空is null
* 查詢沒有填寫地址的學生
~~~
select * from students where hometown is null;
~~~
* 判非空is not null
* 查詢填寫了地址的學生
~~~
select * from students where hometown is not null;
~~~
* 查詢填寫了地址的女生
~~~
select * from students where hometown is not null and gender=0;
~~~
## 優先級
* 小括號,not,比較運算符,邏輯運算符
* and比or先運算,如果同時出現并希望先算or,需要結合()使用
- mysql
- 1.創建庫和表
- 1.1.數據庫簡介
- 1.2.安裝管理
- 1.3.數據完整性
- 1.4.命令腳本操作
- 2.查詢
- 2.1.條件
- 2.2.聚合
- 2.3.分組
- 2.4.排序
- 2.5.分頁
- 3.高級
- 3.1.關系
- 3.2.連接
- 3.3.自關聯
- 3.4.子查詢
- 3.5.內置函數
- 3.6.視圖
- 3.7.事務
- 4.與python交互
- 4.1.交互類型
- 4.2.增改刪
- 4.3.查詢
- 4.4.封裝
- 4.5.用戶登錄
- Nosql簡介
- mongodb
- 1.基本操作
- 1.1.環境安裝
- 1.2.數據庫操作
- 1.3.集合操作
- 1.4.數據類型
- 1.5.數據操作
- 1.6.數據查詢
- 1.6.1.Limit與Skip
- 1.6.2.投影
- 1.6.3.排序
- 1.6.4.統計個數
- 1.6.5.消除重復
- 2.高級操作
- 2.1.聚合aggregate
- 2.1.1.$group
- 2.1.2.$match
- 2.1.3.$project
- 2.1.4.$sort
- 2.1.5.$limit,$skip
- 2.1.6.$unwind
- 2.2.安全
- 2.3.復制(副本集)
- 2.4.備份和恢復
- 2.5.與python交互
- redis
- 1.基本配置
- 2.數據操作
- 2.1.string
- 2.2.鍵命令
- 2.3.hash
- 2.4.list
- 2.5.set
- 2.6.zset
- 4.高級
- 4.1.發布訂閱
- 4.2.主從配置
- 5.與python交互
- 6.login登陸完善