當前位置:首頁 >  站長 >  數據庫 >  正文

解決mysql使用not in 包含null值的問題

 2021-01-07 16:58  來源: 腳本之家   我來投稿 撤稿糾錯

  域名預訂/競價,好“米”不錯過

這篇文章主要介紹了解決mysql使用not in 包含null值的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

注意?。。?/p>

select * from user where uid not in (a,b,c,null);

這個sql不回返回任何結果。要避免not in的list中出現null的情況。

另外:

–如果null參與算術運算,則該算術表達式的值為null。(例如:+,-,*,/ 加減乘除)

–如果null參與比較運算,則結果可視為false。(例如:>=,<=,<> 大于,小于,不等于)

–如果null參與聚集運算,則聚集函數都置為null(使用isnull(字段,0)等方式可以避免這種情況)。除count(*), count(1), count(0)等之外(count(字段) 字段為null的行不參與計數)。

--如果在not in子查詢中有null值的時候,則不會返回數據。

補充:MySQL in,not in,exists,not exists與null的恩恩怨怨

null這個東西在數據里算是個奇葩,在比較中也比較特殊,下面記錄總結一下在in,not in,exists,not exists中null對判斷結果的影響。

做一些描述聲明,在比較符左邊的我們稱為左比較符,在比較符右邊的我們稱為右比較符,例如1 in (1,2),那么in左邊的1是左比較符,in右邊的(1,2)是右比較符。

1.in

1.1當左比較符是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當右比較符包含null,只當左比較符不為null,且右比較符包含左比較符時,返回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當左比較符為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當右比較符包含null,當右比較符包含左比較符時返回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時判斷為真。

mysql> select exists (select null);
+----------------------+
| exists (select null) |
+----------------------+
| 1 |
+----------------------+
1 row in set (0.00 sec)

4.not exists

not exists子查詢返回null時判斷為假。

mysql> select not exists (select null);
+--------------------------+
| not exists (select null) |
+--------------------------+
| 0 |
+--------------------------+
1 row in set (0.00 sec)

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。

來源:腳本之家

鏈接:https://www.jb51.net/article/203319.htm

申請創(chuàng)業(yè)報道,分享創(chuàng)業(yè)好點子。點擊此處,共同探討創(chuàng)業(yè)新機遇!

相關標簽
mysql

相關文章

  • MySQL5.7 集群配置的步驟

    這篇文章主要介紹了MySQL5.7集群配置的步驟,幫助大家更好的理解和學習使用MySQL,感興趣的朋友可以了解下

    標簽:
    mysql
  • MySQL中觸發(fā)器和游標的介紹與使用

    這篇文章主要給大家介紹了關于MySQL中觸發(fā)器和游標的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

    標簽:
    mysql
  • MySQL中exists、in及any的基本用法

    這篇文章主要給大家介紹了關于MySQL中exists、in及any的基本用法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

    標簽:
    mysql
  • MySQL正確修改最大連接數的3種方案

    這篇文章主要給大家介紹了關于MySQL正確修改最大連接數的3種方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

    標簽:
    mysql
  • 教你怎么在Windows系統上手動搭建MySql數據庫

    1.已經購買好的騰訊云輕量應用服務器。2.作者本次演示使用的系統是WindowsServer2019,其他windows版本也可以,操作流程大同小異。3.使用Windows遠程桌面連接騰訊云輕量服務器。

熱門排行

信息推薦