MySQL查詢條件常見用法詳解
阿新 • • 發佈:2020-01-09
本文例項講述了MySQL查詢條件常見用法。分享給大家供大家參考,具體如下:
條件
使用where子句對錶中的資料篩選,結果為true的行會出現在結果集中
語法如下:
select * from 表名 where 條件;
例:
select * from students where id=1;
where後面支援多種運算子,進行條件的處理
比較運算子
邏輯運算子
模糊查詢
範圍查詢
空判斷
比較運算子
等於: =
大於: >
大於等於: >=
小於: <
小於等於: <=
不等於: != 或 <>
例1:查詢編號大於3的學生
select * from students where id > 3;
例2:查詢編號不大於4的學生
select * from students where id <= 4;
例3:查詢姓名不是“黃蓉”的學生
select * from students where name != '黃蓉';
例4:查詢沒被刪除的學生
select * from students where is_delete=0;
邏輯運算子
and
or
not
例5:查詢編號大於3的女同學
select * from students where id > 3 and gender=0;
例6:查詢編號小於4或沒被刪除的學生
select * from students where id < 4 or is_delete=0;
模糊查詢
like
%表示任意多個任意字元
_表示一個任意字元
例7:查詢姓黃的學生
select * from students where name like '黃%';
例8:查詢姓黃並且“名”是一個字的學生
select * from students where name like '黃_';
例9:查詢姓黃或叫靖的學生
select * from students where name like '黃%' or name like '%靖';
範圍查詢
in表示在一個非連續的範圍內
例10:查詢編號是1或3或8的學生
select * from students where id in(1,3,8);
between … and …表示在一個連續的範圍內
例11:查詢編號為3至8的學生
select * from students where id between 3 and 8;
例12:查詢編號是3至8的男生
select * from students where (id between 3 and 8) and gender=1;
空判斷
注意:null與''是不同的
判空is null
例13:查詢沒有填寫身高的學生
select * from students where height is null;
判非空is not null
例14:查詢填寫了身高的學生
select * from students where height is not null;
例15:查詢填寫了身高的男生
select * from students where height is not null and gender=1;
優先順序
優先順序由高到低的順序為:小括號,not,比較運算子,邏輯運算子
and比or先運算,如果同時出現並希望先算or,需要結合()使用
更多關於MySQL相關內容感興趣的讀者可檢視本站專題:《MySQL查詢技巧大全》、《MySQL常用函式大彙總》、《MySQL日誌操作技巧大全》、《MySQL事務操作技巧彙總》、《MySQL儲存過程技巧大全》及《MySQL資料庫鎖相關技巧彙總》
希望本文所述對大家MySQL資料庫計有所幫助。