- where語句:
select id from product where ...
- =:等於
- <>:不等於
- !=:不等於
- <:小於
- <=:小於等於
- >:大於
- >=:大於等於
- is null:空值
- is not null:非空值
- between操作符:在指定兩個值之間(檢索結果包含兩個邊界值)
select id from product where id=1;
查找id爲1的產品id
select id from product where name="榴蓮牛奶";
查找name爲榴蓮牛奶的產品id
select id from product where id<>3;
查找id不是3的產品id
select id from product where name<>"coco牛奶"
查找產品名稱不是coco牛奶的產品id
select id from product where price>=20;
查找價格大於等於20的產品id
select id from product where name is null;
查找產品名稱爲空的產品id
select id from product where name is not null;
查找產品名稱非空的產品id
select id,name,price from product where price between 5 and 10
查找產品價格5<=,並且>=10 的產品id,name,price
- and操作符:並列條件,都滿足纔可以被檢索出來
- or操作符:或者條件,只要一個滿足即可被檢索出來
- 計算次序:and>or
select id from product where price>10 and channel=1
查找產品價格大於10 且渠道時1的產品id
select id from product where price>10 or channel=1
查找產品價格大於10或者渠道時1的產品id
select id from product where price>10 or channel=1 and madein="China";
相當於:
select id from product where price>10 or (channel=1 and madein="China");
查詢結果滿足價格大於10或者滿足()內的條件即可
- in ()操作符:指定條件範圍,只要匹配範圍中的任意一個條件即可
*** in (數字1,2,3…)或者 in (‘字符1’,‘字符2’,‘字符3’…)
select id from product where price in (10,20);
檢索價格時10或者是20的產品id
select id from product where type in ('網紅','零食','休閒');
檢索標籤中含有“網紅"、“零食”、"休閒"的產品id
通配符進行過濾
- like操作符
1、百分號%通配符:表示代表任意個字符(該通配符可以代表0個字符或任意個字符
如:找出名稱中符合“****鼓浪嶼海島酒店”的產品:
select id,name from product where name like "%鼓浪嶼海島酒店"
即可找到“廈門鼓浪嶼海島酒店”(因爲“鼓浪嶼”前有%,可以表示多個字符)
2、下劃線_通配符:表示代表一個字符(有幾個_就代表幾個字符)
如:找到名稱中符合“鼓浪嶼海島酒店的產品”
select id ,name from product where name like"_鼓浪嶼海島酒店"
只有一個_找不到"廈門鼓浪嶼海島酒店”
select id,name from product where name like"__鼓浪嶼海島酒店"
可找到“廈門鼓浪嶼海島酒店”(因爲“鼓浪嶼”前有兩個下劃線)
3、備註:mysql本身是不區分字母的大小寫的, 可使用binary來區分字母的大小寫
如:
select id,name from product where name like"tom";
此時可以找到name爲Tom的產品(因爲默認不區分大小寫的)
select id,name from product where binary name like"tom";
此時不能找到名稱爲Tom的產品(因爲binary區分了大小寫)
4、備註:不要過度使用通配符,因爲使用通配符條件檢索速度很慢
5、備註:通配符的位置,最好放在搜索模式的最後面,因爲放在開始的位置,檢索速度會很慢