您好,欢迎来到聚站网!
当前位置:聚站网 » 站长资讯 » 互联网资讯 » 优化推广 » 文章详细

解决mysql使用not in 包含null值的问题

来源:网络 浏览:84次 时间:2021-10-12

这篇文章主要介绍了解决mysql使用not in 包含null值的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

注意!!!

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)

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。

推荐站点

  • 高仿包高仿包

    竭誠精品-高仿世界名牌包包專賣網主營高仿LV包,高仿GUCCI古奇包,高仿HERMES愛馬仕,高仿CHANEL香奈兒,高仿PRADA,高仿巴黎世家等國際高仿包包、1比1高仿女包、高仿包、精仿奢侈品、世界名包、皮帶、皮具、手表、飾品等。LV新款包包同步香港LV專櫃及LV官方網站,LV行業最好品質!

    awhso.net
  • YY分类目录YY分类目录

    YY分类目录全人工编辑的开放式网站分类目录,收录国内外、各行业优秀网站,旨在为用户提供网站分类目录检索、优秀网站参考、网站推广服务。

    www.yydir.com
  • 70网站目录70网站目录

    全人工编辑的开放式网站分类目录,免费收录国内外、各行业优秀网站,旨在为用户提供网站分类目录检索、优秀网站参考、网站推广服务。

    www.70dir.com
  • 25分类目录25分类目录

    25分类目录专业提供网站网址免费提交收录,25分类目录是采用开放导航式的网站大全,收录国内外各行业优秀的网站网址,让网站在各大搜索引擎收录快排名靠前。

    www.25dir.com
  • 99网站目录99网站目录

    久久网站目录采用全人工编辑的开放式网站分类目录,久久网站大全免费收录国内外、各行业优秀网站网址,旨在为用户提供网站分类目录检索、优秀网站参考、网站推广服务。

    https://www.99dir.com