共計 1448 個字符,預計需要花費 4 分鐘才能閱讀完成。
自動寫代碼機器人,免費開通
今天就跟大家聊聊有關 MySQL 中有哪些查詢條件,可能很多人都不太了解,為了讓大家更加了解,丸趣 TV 小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
條件
使用 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 中有哪些查詢條件有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注丸趣 TV 行業資訊頻道,感謝大家的支持。
向 AI 問一下細節