解決mysql使用not in 包含null值的問題
注意?。?!
select * from user where uid not in (a,b,c,null);
這個(gè)sql不回返回任何結(jié)果。要避免not in的list中出現(xiàn)null的情況。
另外:
–如果null參與算術(shù)運(yùn)算,則該算術(shù)表達(dá)式的值為null。(例如:+,-,*,/ 加減乘除)
–如果null參與比較運(yùn)算,則結(jié)果可視為false。(例如:>=,<=,<> 大于,小于,不等于)
–如果null參與聚集運(yùn)算,則聚集函數(shù)都置為null(使用isnull(字段,0)等方式可以避免這種情況)。除count(*), count(1), count(0)等之外(count(字段) 字段為null的行不參與計(jì)數(shù))。
--如果在not in子查詢中有null值的時(shí)候,則不會(huì)返回?cái)?shù)據(jù)。
補(bǔ)充:MySQL in,not in,exists,not exists與null的恩恩怨怨
null這個(gè)東西在數(shù)據(jù)里算是個(gè)奇葩,在比較中也比較特殊,下面記錄總結(jié)一下在in,not in,exists,not exists中null對判斷結(jié)果的影響。
做一些描述聲明,在比較符左邊的我們稱為左比較符,在比較符右邊的我們稱為右比較符,例如1 in (1,2),那么in左邊的1是左比較符,in右邊的(1,2)是右比較符。
1.in
1.1當(dāng)左比較符是null,任何情況下都返回null。
mysql> select null in (1,2); +---------------+ | null in (1,2) | +---------------+ | NULL | +---------------+ 1 row in set (0.00 sec) mysql> select null in (1,2,null); +--------------------+ | null in (1,2,null) | +--------------------+ | NULL | +--------------------+ 1 row in set (0.00 sec)
1.2當(dāng)右比較符包含null,只當(dāng)左比較符不為null,且右比較符包含左比較符時(shí),返回1,其他情況均返回null。
mysql> select null in (1,2,null); +--------------------+ | null in (1,2,null) | +--------------------+ | NULL | +--------------------+ 1 row in set (0.00 sec) mysql> select 3 in (1,2,null); +-----------------+ | 3 in (1,2,null) | +-----------------+ | NULL | +-----------------+ 1 row in set (0.00 sec) mysql> select 1 in (1,2,null); +-----------------+ | 1 in (1,2,null) | +-----------------+ | 1 | +-----------------+ 1 row in set (0.00 sec)
2.not in
2.1當(dāng)左比較符為null,任何情況都返回null。
mysql> select null not in (1,2,null); +------------------------+ | null not in (1,2,null) | +------------------------+ | NULL | +------------------------+ 1 row in set (0.00 sec) mysql> select null not in (1,2); +-------------------+ | null not in (1,2) | +-------------------+ | NULL | +-------------------+ 1 row in set (0.00 sec)
2.2當(dāng)右比較符包含null,當(dāng)右比較符包含左比較符時(shí)返回0,其他情況均返回null。
mysql> select 1 not in (1,2,null); +---------------------+ | 1 not in (1,2,null) | +---------------------+ | 0 | +---------------------+ 1 row in set (0.00 sec) mysql> select 1 not in (2,3,null); +---------------------+ | 1 not in (2,3,null) | +---------------------+ | NULL | +---------------------+ 1 row in set (0.00 sec)
3.exists
exists子查詢返回null時(shí)判斷為真。
mysql> select exists (select null); +----------------------+ | exists (select null) | +----------------------+ | 1 | +----------------------+ 1 row in set (0.00 sec)
4.not exists
not exists子查詢返回null時(shí)判斷為假。
mysql> select not exists (select null); +--------------------------+ | not exists (select null) | +--------------------------+ | 0 | +--------------------------+ 1 row in set (0.00 sec)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
MySQL登錄時(shí)出現(xiàn)ERROR 1045: Access denied for&
本文已解決MySQL登錄時(shí)出現(xiàn)Access denied for user ‘root‘@‘localhost‘ (using password: YES)無法打開的相關(guān)報(bào)錯(cuò)問題,并總結(jié)提出了幾種可用解決方案,又遇到同樣問題的朋友可以參考閱讀下本文2024-09-09
利用JuiceFS使MySQL?備份驗(yàn)證性能提升?10?倍
這篇文章主要介紹了如何讓?MySQL?備份驗(yàn)證性能提升?10?倍,JuiceFS?非常適合用來做?MySQL?物理備份,通過不斷調(diào)整?XtraBackup?的參數(shù)和?JuiceFS?的掛載參數(shù),在一個(gè)小時(shí)內(nèi)將時(shí)間縮短到原先的?1/10,下文一起來看相關(guān)內(nèi)容的詳細(xì)介紹吧2022-03-03
mysql8.0?my.ini?如何永久修改時(shí)區(qū)
這篇文章主要介紹了mysql8.0?my.ini?如何永久修改時(shí)區(qū),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
mysql where中如何判斷不為空的實(shí)現(xiàn)
本文主要介紹了mysql where中如何判斷不為空的實(shí)現(xiàn),本文將針對這些空演示如何判斷是否為空,以及如何寫sql過濾,包括使用判空函數(shù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-03-03
update.where無索引導(dǎo)致MySQL死鎖問題解決
這篇文章主要為大家介紹了update.where無索引導(dǎo)致MySQL死鎖問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11

