運(yùn)算符 | 說(shuō)明 |
= | 等于 |
> 或者 != | 不等于 |
> | 大于 |
>= | 大于等于 |
小于 | |
= | 小于等于 |
關(guān)系運(yùn)算基本的語(yǔ)法格式如下:
select cname1,cname2,... from tname where cname operators cval
等于=
查詢出 列和后面的值嚴(yán)格相等的數(shù)據(jù),非值類(lèi)型的需要對(duì)后面值加上引號(hào),值類(lèi)型的不需要。
語(yǔ)法格式如下:
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
不等于(>、!=)
不等于有兩種寫(xiě)法,一種是>,另一種是!=,意思一樣,可隨意切換使用,但是 > 先于 != 出現(xiàn),所以看很多以前的例子,> 出現(xiàn)頻率比較高,可移植性更強(qiáng),推薦使用。
不等于的目的是查詢出與條件不符和結(jié)果,格式如下:
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
大于小于(> )
一般用于數(shù)值或者日期、時(shí)間類(lèi)型的比較,格式如下:
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 age21; +----+-------+-----+----------+-----+ | 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
邏輯運(yùn)算
運(yùn)算符 | 說(shuō)明 |
---|---|
AND | 多個(gè)條件都成立 |
OR | 多個(gè)條件中滿足一個(gè) |
NOT | 對(duì)條件進(jìn)行取非操作 |
AND(且)
當(dāng)需要多個(gè)條件進(jìn)行數(shù)據(jù)過(guò)濾的時(shí)候,使用這種方式,and的每個(gè)表達(dá)式都是要成立,過(guò)濾出來(lái)的數(shù)據(jù)就是用戶需要的。
下面過(guò)濾出年齡和性別兩個(gè)條件都成立的數(shù)據(jù),語(yǔ)法格式如下:
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(或)
當(dāng)多個(gè)條件中只要滿足一個(gè)條件即進(jìn)行數(shù)據(jù)過(guò)濾。
下面條件過(guò)濾出年齡大于21歲和小于21歲的數(shù)據(jù),語(yǔ)法格式如下:
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 age21; +----+-------+-----+----------+-----+ | id | name | age | address | sex | +----+-------+-----+----------+-----+ | 2 | helen | 20 | quanzhou | 0 | | 4 | weng | 33 | guizhou | 1 | +----+-------+-----+----------+-----+ 2 rows in set
NOT(取非)
對(duì)某個(gè)滿足的條件進(jìn)行取反,過(guò)濾出來(lái)的數(shù)據(jù)就是用戶需要的。
下面過(guò)濾不屬于年齡大于20的數(shù)據(jù),語(yǔ)法格式如下:
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
模糊匹配
就像我們上面的那個(gè)用戶表信息表(包含名稱(chēng)、年齡、地址、性別),當(dāng)我們要查詢名稱(chēng)為s開(kāi)頭的用戶時(shí),就可以用到 like 關(guān)鍵字了,他用以模糊匹配數(shù)據(jù)。
語(yǔ)法格式如下,pattern中可以包含通配符,有兩種。%:表示匹配任意一個(gè)或n個(gè)字符; _:表示匹配任意一個(gè)字符。
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
注意點(diǎn)
1、不要過(guò)度使用模糊匹配得通配符。如果其他操作符能達(dá)到相同的目的,應(yīng)該使用其他操作符
2、對(duì)大體量的表進(jìn)行模糊匹配的時(shí)候盡量不要以%開(kāi)頭,比如 like '%username',這樣會(huì)執(zhí)行掃表,效率較慢。盡量明確模糊查找的開(kāi)頭部分,比如 like 'brand%',會(huì)先定位到brand開(kāi)頭的數(shù)據(jù),效率高很多。
范圍值檢查
BETWEEN AND(區(qū)間查詢)
操作符 BETWEEN … AND 會(huì)選取介于兩個(gè)值之間的數(shù)據(jù)范圍,這些值可以是數(shù)值、文本或者日期,屬于一個(gè)閉區(qū)間查詢。
and 的左邊val1 和 右邊 val2 分別表示兩個(gè)臨界值,等同于數(shù)學(xué)公式[val1,val2] ,屬于這兩個(gè)區(qū)間的數(shù)據(jù)會(huì)被過(guò)濾出來(lái)(>=val1 和 =val2),所以語(yǔ)法格式如下:
selec cname1,cname2,... from tname where cname between val1 and val2; 等同于 selec cname1,cname2,... from tname where cname >= val1 and cname = val2;
查詢年齡在[21,25]之間的數(shù)據(jù):
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(包含查詢)
按照上面得數(shù)據(jù),如果我們想查出居住地位于福州和廈門(mén)得用戶數(shù)據(jù),應(yīng)該使用 IN操作符,因?yàn)?IN 操作符允許我們?cè)?WHERE 子句中指定多個(gè)值,符合這些值中得某一項(xiàng),既滿足條件返回?cái)?shù)據(jù)。
語(yǔ)法格式如下,in 后面列表的值類(lèi)型必須一致或兼容,且不支持通配符:
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(對(duì)包含查詢?nèi)》矗?/strong>
我們上面已經(jīng)學(xué)習(xí)過(guò)了not得用戶,對(duì)not后面執(zhí)行得表達(dá)式進(jìn)行取反得操作,測(cè)試下:
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
判斷是否為空,語(yǔ)法格式如下,這邊注意的是,對(duì)值為null的數(shù)據(jù),各種比較運(yùn)算符、like、between and、in、not in查詢都不起作用,只有is null 能夠過(guò)濾出來(lái)。
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
有一種關(guān)鍵字 =>,可以包含對(duì)null值得判斷,但是目前用的比較少了,有興趣可以去查查,這邊不贅述。
總結(jié)
1、like表達(dá)式中的%匹配一個(gè)到多個(gè)任意字符,_匹配一個(gè)任意字符
2、空值查詢需要使用IS NULL或者IS NOT NULL,其他查詢運(yùn)算符對(duì)NULL值無(wú)效。即使%通配符可以匹配任何東西,也不能匹配值NULL的數(shù)據(jù)。
3、建議創(chuàng)建表的時(shí)候,表字段不設(shè)置空,給字段一個(gè)default 默認(rèn)值。
4、MySQL支持使用NOT對(duì)IN 、BETWEEN 和EXISTS子句取反 。
到此這篇關(guān)于MySQL全面瓦解之查詢的過(guò)濾條件的文章就介紹到這了,更多相關(guān)MySQL查詢的過(guò)濾條件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
標(biāo)簽:惠州 牡丹江 合肥 天津 阿里 呼和浩特 沈陽(yáng) 公主嶺
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《MySQL全面瓦解之查詢的過(guò)濾條件詳解》,本文關(guān)鍵詞 MySQL,全面,瓦解,之,查詢,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。