您好,登錄后才能下訂單哦!
MySQL數據庫中如何實現通過條件查詢?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
概述
在實際的業務場景應用中,我們經常要根據業務條件獲取并篩選出我們的目標數據。這個過程我們稱之為數據查詢的過濾。而過濾過程使用的各種條件(比如日期時間、用戶、狀態)是我們獲取精準數據的必要步驟,
這樣才能得到我們期望的結果。所以本章我們來學習MySQL中查詢過濾條件的各種用法。
關系運算
關系運算就是where語句后跟上一個或者n個條件,滿足where后面條件的數據會被返回,反之不滿足的就會被過濾掉。operators指的是運算符 ,有如下幾種情況:
運算符 | 說明 |
= | 等于 |
<> 或者 != | 不等于 |
> | 大于 |
>= | 大于等于 |
< | 小于 |
<= | 小于等于 |
關系運算基本的語法格式如下:
select cname1,cname2,... from tname where cname operators cval
等于=
查詢出 列和后面的值嚴格相等的數據,非值類型的需要對后面值加上引號,值類型的不需要。
語法格式如下:
select cname1,cname2,... from tname where cname = cval;
mysql> select * from user2; +----+-------+-----+----------+-----+ | id | name | age | address | sex | +----+-------+-----+----------+-----+ | 1 | brand | 21 | fuzhou | 1 | | 2 | helen | 20 | quanzhou | 0 | | 3 | sol | 21 | xiamen | 0 | +----+-------+-----+----------+-----+ 3 rows in set mysql> select * from user2 where name='helen'; +----+-------+-----+----------+-----+ | id | name | age | address | sex | +----+-------+-----+----------+-----+ | 2 | helen | 20 | quanzhou | 0 | +----+-------+-----+----------+-----+ 1 row in set mysql> select * from user2 where age=21; +----+-------+-----+---------+-----+ | id | name | age | address | sex | +----+-------+-----+---------+-----+ | 1 | brand | 21 | fuzhou | 1 | | 3 | sol | 21 | xiamen | 0 | +----+-------+-----+---------+-----+ 2 rows in set
不等于(<>、!=)
不等于有兩種寫法,一種是<>,另一種是!=,意思一樣,可隨意切換使用,但是 <> 先于 != 出現,所以看很多以前的例子,<> 出現頻率比較高,可移植性更強,推薦使用。
不等于的目的是查詢出與條件不符和結果,格式如下:
select cname1,cname2,... from tname where cname <> cval; 或 select cname1,cname2,... from tname where cname != cval;
mysql> select * from user2; +----+-------+-----+----------+-----+ | id | name | age | address | sex | +----+-------+-----+----------+-----+ | 1 | brand | 21 | fuzhou | 1 | | 2 | helen | 20 | quanzhou | 0 | | 3 | sol | 21 | xiamen | 0 | +----+-------+-----+----------+-----+ 3 rows in set mysql> select * from user2 where age<>20; +----+-------+-----+---------+-----+ | id | name | age | address | sex | +----+-------+-----+---------+-----+ | 1 | brand | 21 | fuzhou | 1 | | 3 | sol | 21 | xiamen | 0 | +----+-------+-----+---------+-----+ 2 rows in set
大于小于(> <)
一般用于數值或者日期、時間類型的比較,格式如下:
select cname1,cname2,... from tname where cname > cval; select cname1,cname2,... from tname where cname < cval; select cname1,cname2,... from tname where cname >= cval; select cname1,cname2,... from tname where cname <= cval;
mysql> select * from user2 where age>20; +----+-------+-----+---------+-----+ | id | name | age | address | sex | +----+-------+-----+---------+-----+ | 1 | brand | 21 | fuzhou | 1 | | 3 | sol | 21 | xiamen | 0 | +----+-------+-----+---------+-----+ 2 rows in set mysql> select * from user2 where age>=20; +----+-------+-----+----------+-----+ | id | name | age | address | sex | +----+-------+-----+----------+-----+ | 1 | brand | 21 | fuzhou | 1 | | 2 | helen | 20 | quanzhou | 0 | | 3 | sol | 21 | xiamen | 0 | +----+-------+-----+----------+-----+ 3 rows in set mysql> select * from user2 where age<21; +----+-------+-----+----------+-----+ | id | name | age | address | sex | +----+-------+-----+----------+-----+ | 2 | helen | 20 | quanzhou | 0 | +----+-------+-----+----------+-----+ 1 row in set mysql> select * from user2 where age<=21; +----+-------+-----+----------+-----+ | id | name | age | address | sex | +----+-------+-----+----------+-----+ | 1 | brand | 21 | fuzhou | 1 | | 2 | helen | 20 | quanzhou | 0 | | 3 | sol | 21 | xiamen | 0 | +----+-------+-----+----------+-----+ 3 rows in set
邏輯運算
運算符 | 說明 |
---|---|
AND | 多個條件都成立 |
OR | 多個條件中滿足一個 |
NOT | 對條件進行取非操作 |
AND(且)
當需要多個條件進行數據過濾的時候,使用這種方式,and的每個表達式都是要成立,過濾出來的數據就是用戶需要的。
下面過濾出年齡和性別兩個條件都成立的數據,語法格式如下:
select cname1,cname2,... from tname where cname1 operators cval1 and cname2 operators cval2
mysql> select * from user2; +----+-------+-----+----------+-----+ | id | name | age | address | sex | +----+-------+-----+----------+-----+ | 1 | brand | 21 | fuzhou | 1 | | 2 | helen | 20 | quanzhou | 0 | | 3 | sol | 21 | xiamen | 0 | | 4 | weng | 33 | guizhou | 1 | +----+-------+-----+----------+-----+ 4 rows in set mysql> select * from user2 where age >20 and sex=1; +----+-------+-----+---------+-----+ | id | name | age | address | sex | +----+-------+-----+---------+-----+ | 1 | brand | 21 | fuzhou | 1 | | 4 | weng | 33 | guizhou | 1 | +----+-------+-----+---------+-----+ 2 rows in set
OR(或)
當多個條件中只要滿足一個條件即進行數據過濾。
下面條件過濾出年齡大于21歲和小于21歲的數據,語法格式如下:
select cname1,cname2,... from tname where cname1 operators cval1 or cname2 operators cval2
mysql> select * from user2; +----+-------+-----+----------+-----+ | id | name | age | address | sex | +----+-------+-----+----------+-----+ | 1 | brand | 21 | fuzhou | 1 | | 2 | helen | 20 | quanzhou | 0 | | 3 | sol | 21 | xiamen | 0 | | 4 | weng | 33 | guizhou | 1 | +----+-------+-----+----------+-----+ 4 rows in set mysql> select * from user2 where age>21 or age<21; +----+-------+-----+----------+-----+ | id | name | age | address | sex | +----+-------+-----+----------+-----+ | 2 | helen | 20 | quanzhou | 0 | | 4 | weng | 33 | guizhou | 1 | +----+-------+-----+----------+-----+ 2 rows in set
NOT(取非)
對某個滿足的條件進行取反,過濾出來的數據就是用戶需要的。
下面過濾不屬于年齡大于20的數據,語法格式如下:
select cname1,cname2,... from tname where not(cname operators cval)
mysql> select * from user2; +----+-------+-----+----------+-----+ | id | name | age | address | sex | +----+-------+-----+----------+-----+ | 1 | brand | 21 | fuzhou | 1 | | 2 | helen | 20 | quanzhou | 0 | | 3 | sol | 21 | xiamen | 0 | | 4 | weng | 33 | guizhou | 1 | +----+-------+-----+----------+-----+ 4 rows in set mysql> select * from user2 where not(age>20); +----+-------+-----+----------+-----+ | id | name | age | address | sex | +----+-------+-----+----------+-----+ | 2 | helen | 20 | quanzhou | 0 | +----+-------+-----+----------+-----+ 1 row in set
模糊匹配
就像我們上面的那個用戶表信息表(包含名稱、年齡、地址、性別),當我們要查詢名稱為s開頭的用戶時,就可以用到 like 關鍵字了,他用以模糊匹配數據。
語法格式如下,pattern中可以包含通配符,有兩種。%:表示匹配任意一個或n個字符; _:表示匹配任意一個字符。
select cname1,cname2,... from tname where cname like pattern;
%的使用
mysql> select * from user2; +----+--------+-----+----------+-----+ | id | name | age | address | sex | +----+--------+-----+----------+-----+ | 1 | brand | 21 | fuzhou | 1 | | 2 | helen | 20 | quanzhou | 0 | | 3 | sol | 21 | xiamen | 0 | | 4 | weng | 33 | guizhou | 1 | | 5 | selina | 25 | taiwang | 0 | +----+--------+-----+----------+-----+ 5 rows in set mysql> select * from user2 where name like 's%'; +----+--------+-----+---------+-----+ | id | name | age | address | sex | +----+--------+-----+---------+-----+ | 3 | sol | 21 | xiamen | 0 | | 5 | selina | 25 | taiwang | 0 | +----+--------+-----+---------+-----+ 2 rows in set
_的使用
mysql> select * from user2; +----+--------+-----+----------+-----+ | id | name | age | address | sex | +----+--------+-----+----------+-----+ | 1 | brand | 21 | fuzhou | 1 | | 2 | helen | 20 | quanzhou | 0 | | 3 | sol | 21 | xiamen | 0 | | 4 | weng | 33 | guizhou | 1 | | 5 | selina | 25 | taiwang | 0 | +----+--------+-----+----------+-----+ 5 rows in set mysql> select * from user2 where name like 's_l'; +----+------+-----+---------+-----+ | id | name | age | address | sex | +----+------+-----+---------+-----+ | 3 | sol | 21 | xiamen | 0 | +----+------+-----+---------+-----+ 1 row in set
注意點
1、不要過度使用模糊匹配得通配符。如果其他操作符能達到相同的目的,應該使用其他操作符
2、對大體量的表進行模糊匹配的時候盡量不要以%開頭,比如 like '%username',這樣會執行掃表,效率較慢。盡量明確模糊查找的開頭部分,比如 like 'brand%',會先定位到brand開頭的數據,效率高很多。
范圍值檢查
BETWEEN AND(區間查詢)
操作符 BETWEEN … AND 會選取介于兩個值之間的數據范圍,這些值可以是數值、文本或者日期,屬于一個閉區間查詢。
and 的左邊val1 和 右邊 val2 分別表示兩個臨界值,等同于數學公式[val1,val2] ,屬于這兩個區間的數據會被過濾出來(>=val1 和 <=val2),所以語法格式如下:
selec cname1,cname2,... from tname where cname between val1 and val2; 等同于 selec cname1,cname2,... from tname where cname >= val1 and cname <= val2;
查詢年齡在[21,25]之間的數據:
mysql> select * from user2; +----+--------+-----+----------+-----+ | id | name | age | address | sex | +----+--------+-----+----------+-----+ | 1 | brand | 21 | fuzhou | 1 | | 2 | helen | 20 | quanzhou | 0 | | 3 | sol | 21 | xiamen | 0 | | 4 | weng | 33 | guizhou | 1 | | 5 | selina | 25 | taiwang | 0 | +----+--------+-----+----------+-----+ 5 rows in set mysql> select * from user2 where age between 21 and 25; +----+--------+-----+---------+-----+ | id | name | age | address | sex | +----+--------+-----+---------+-----+ | 1 | brand | 21 | fuzhou | 1 | | 3 | sol | 21 | xiamen | 0 | | 5 | selina | 25 | taiwang | 0 | +----+--------+-----+---------+-----+ 3 rows in set mysql> select * from user2 where age >= 21 and age <= 25; +----+--------+-----+---------+-----+ | id | name | age | address | sex | +----+--------+-----+---------+-----+ | 1 | brand | 21 | fuzhou | 1 | | 3 | sol | 21 | xiamen | 0 | | 5 | selina | 25 | taiwang | 0 | +----+--------+-----+---------+-----+ 3 rows in set
IN(包含查詢)
按照上面得數據,如果我們想查出居住地位于福州和廈門得用戶數據,應該使用 IN操作符,因為 IN 操作符允許我們在 WHERE 子句中指定多個值,符合這些值中得某一項,既滿足條件返回數據。
語法格式如下,in 后面列表的值類型必須一致或兼容,且不支持通配符:
select cname1,cname2,... from tname where cname in (val1,val2,...);
mysql> select * from user2; +----+--------+-----+----------+-----+ | id | name | age | address | sex | +----+--------+-----+----------+-----+ | 1 | brand | 21 | fuzhou | 1 | | 2 | helen | 20 | quanzhou | 0 | | 3 | sol | 21 | xiamen | 0 | | 4 | weng | 33 | guizhou | 1 | | 5 | selina | 25 | taiwang | 0 | +----+--------+-----+----------+-----+ 5 rows in set mysql> select * from user2 where address in('fuzhou','xiamen'); +----+-------+-----+---------+-----+ | id | name | age | address | sex | +----+-------+-----+---------+-----+ | 1 | brand | 21 | fuzhou | 1 | | 3 | sol | 21 | xiamen | 0 | +----+-------+-----+---------+-----+ 2 rows in set
NOT IN(對包含查詢取反)
我們上面已經學習過了not得用戶,對not后面執行得表達式進行取反得操作,測試下:
mysql> select * from user2; +----+--------+-----+----------+-----+ | id | name | age | address | sex | +----+--------+-----+----------+-----+ | 1 | brand | 21 | fuzhou | 1 | | 2 | helen | 20 | quanzhou | 0 | | 3 | sol | 21 | xiamen | 0 | | 4 | weng | 33 | guizhou | 1 | | 5 | selina | 25 | taiwang | 0 | +----+--------+-----+----------+-----+ 5 rows in set mysql> select * from user2 where address not in('fuzhou','quanzhou','xiamen'); +----+--------+-----+---------+-----+ | id | name | age | address | sex | +----+--------+-----+---------+-----+ | 4 | weng | 33 | guizhou | 1 | | 5 | selina | 25 | taiwang | 0 | +----+--------+-----+---------+-----+ 2 rows in set
空值檢查
IS NULL/IS NOT NULL
判斷是否為空,語法格式如下,這邊注意的是,對值為null的數據,各種比較運算符、like、between and、in、not in查詢都不起作用,只有is null 能夠過濾出來。
select cname1,cname2,... from tname where cname is null; 或者 select cname1,cname2,... from tname where cname is not null;
mysql> select * from user2 where address is null; +----+--------+-----+---------+-----+ | id | name | age | address | sex | +----+--------+-----+---------+-----+ | 5 | selina | 25 | NULL | 0 | +----+--------+-----+---------+-----+ 1 row in set mysql> select * from user2 where address is not null; +----+-------+-----+----------+-----+ | id | name | age | address | sex | +----+-------+-----+----------+-----+ | 1 | brand | 21 | fuzhou | 1 | | 2 | helen | 20 | quanzhou | 0 | | 3 | sol | 21 | xiamen | 0 | | 4 | weng | 33 | guizhou | 1 | +----+-------+-----+----------+-----+ 4 rows in set
有一種關鍵字 <=>,可以包含對null值得判斷,但是目前用的比較少了,有興趣可以去查查,這邊不贅述。
總結
1、like表達式中的%匹配一個到多個任意字符,_匹配一個任意字符
2、空值查詢需要使用IS NULL或者IS NOT NULL,其他查詢運算符對NULL值無效。即使%通配符可以匹配任何東西,也不能匹配值NULL的數據。
3、建議創建表的時候,表字段不設置空,給字段一個default 默認值。
4、MySQL支持使用NOT對IN 、BETWEEN 和EXISTS子句取反 。
關于MySQL數據庫中如何實現通過條件查詢問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。