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