本文實例講述了MySQL查詢條件常見用法。分享給大家供大家參考,具體如下:
條件
使用where子句對表中的數(shù)據(jù)篩選,結(jié)果為true的行會出現(xiàn)在結(jié)果集中
語法如下:
select * from 表名 where 條件;
例:
select * from students where id=1;
where后面支持多種運算符,進行條件的處理
比較運算符
邏輯運算符
模糊查詢
范圍查詢
空判斷
比較運算符
等于: =
大于: >
大于等于: >=
小于:
小于等于: =
不等于: != 或 >
例1:查詢編號大于3的學(xué)生
select * from students where id > 3;
例2:查詢編號不大于4的學(xué)生
select * from students where id = 4;
例3:查詢姓名不是“黃蓉”的學(xué)生
select * from students where name != '黃蓉';
例4:查詢沒被刪除的學(xué)生
select * from students where is_delete=0;
邏輯運算符
and
or
not
例5:查詢編號大于3的女同學(xué)
select * from students where id > 3 and gender=0;
例6:查詢編號小于4或沒被刪除的學(xué)生
select * from students where id 4 or is_delete=0;
模糊查詢
like
%表示任意多個任意字符
_表示一個任意字符
例7:查詢姓黃的學(xué)生
select * from students where name like '黃%';
例8:查詢姓黃并且“名”是一個字的學(xué)生
select * from students where name like '黃_';
例9:查詢姓黃或叫靖的學(xué)生
select * from students where name like '黃%' or name like '%靖';
范圍查詢
in表示在一個非連續(xù)的范圍內(nèi)
例10:查詢編號是1或3或8的學(xué)生
select * from students where id in(1,3,8);
between … and …表示在一個連續(xù)的范圍內(nèi)
例11:查詢編號為3至8的學(xué)生
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:查詢沒有填寫身高的學(xué)生
select * from students where height is null;
判非空is not null
例14:查詢填寫了身高的學(xué)生
select * from students where height is not null;
例15:查詢填寫了身高的男生
select * from students where height is not null and gender=1;
優(yōu)先級
優(yōu)先級由高到低的順序為:小括號,not,比較運算符,邏輯運算符
and比or先運算,如果同時出現(xiàn)并希望先算or,需要結(jié)合()使用
更多關(guān)于MySQL相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《MySQL查詢技巧大全》、《MySQL常用函數(shù)大匯總》、《MySQL日志操作技巧大全》、《MySQL事務(wù)操作技巧匯總》、《MySQL存儲過程技巧大全》及《MySQL數(shù)據(jù)庫鎖相關(guān)技巧匯總》
希望本文所述對大家MySQL數(shù)據(jù)庫計有所幫助。
您可能感興趣的文章:- mysql查詢條件not in 和 in的區(qū)別及原因說明
- MySQL全面瓦解之查詢的過濾條件詳解
- mysql條件查詢and or使用方法及優(yōu)先級實例分析
- 詳解Mysql查詢條件中字符串尾部有空格也能匹配上的問題
- MySQL查詢條件中in會用到索引嗎
- mysql 帶多個條件的查詢方式