【问题标题】:MySQL: does "NOT IN()" function not allow 'NULL' values in the results set?MySQL:“NOT IN()”函数在结果集中不允许“NULL”值吗?
【发布时间】:2012-04-19 02:52:49
【问题描述】:

我的本​​地服务器上有一个简单的表。

mysql> desc table ;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(10) | YES  |     | NULL    |       | 
| count | int(10) | YES  |     | NULL    |       | 
+-------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec

它只有三个记录。

mysql> select * from uday ;
+------+-------+
| id   | count |
+------+-------+
|    1 |     1 | 
|    2 |     2 | 
|    3 |     0 | 
|    4 |  NULL | 
+------+-------+
4 rows in set (0.00 sec)

现在,为什么我在下面的结果中没有看到第四列..?

mysql> select * from uday where count NOT IN (0) ;
mysql> select * from uday where count != 0 ;
+------+-------+
| id   | count |
+------+-------+
|    1 |     1 | 
|    2 |     2 | 
+------+-------+
2 rows in set (0.00 sec)

第四条记录怎么样...?它在结果中不可见。 NULL 不是 0 RIGHT...?

如果它看起来很愚蠢,请忽略,因为我在编码部分甚至没有竞争力。

【问题讨论】:

    标签: mysql notnull


    【解决方案1】:
    col1 not in (1,2,null)
    

    是以下的简写:

    col1 <> 1 and col1 <> 2 and col1 <> null
    

    在 SQL 的 three-valued logic 中,col1 &lt;&gt; null 返回 unknown。并且true and true and unknown 也返回unknown。由于where 只接受true,而不接受unknown,所以null 所在的行被从结果集中过滤掉。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-17
      • 2013-07-26
      • 2019-09-03
      • 2022-01-19
      • 2012-08-06
      • 1970-01-01
      • 2013-01-06
      相关资源
      最近更新 更多