为什么 "where not in" 查询有 0 个结果
Why 0 results on "where not in" query
mysql> show columns in m like 'fld';
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| fld | varchar(45) | YES | MUL | NULL | |
+-------+-------------+------+-----+---------+-------+
1 row in set (0.02 sec)
mysql> show columns in i like 'fld';
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| fld | varchar(45) | YES | MUL | NULL | |
+-------+-------------+------+-----+---------+-------+
1 row in set (0.02 sec)
mysql> select count(distinct fld) from i;
+---------------------+
| count(distinct fld) |
+---------------------+
| 27988 |
+---------------------+
1 row in set (0.03 sec)
mysql> select count(distinct fld) from m;
+---------------------+
| count(distinct fld) |
+---------------------+
| 72558 |
+---------------------+
1 row in set (0.07 sec)
根据我对相关表格的了解,上述结果似乎是合理的。
mysql> select count(*) from m where fld not in (select fld from i);
+----------+
| count(*) |
+----------+
| 0 |
+----------+
1 row in set (0.11 sec)
最后那个结果好像不太合理。 There must be 行在 m
中 fld
不在 i
中!有人可以解释为什么我得到 0
作为结果吗?
为了完整性(因为我怀疑它可能是相关的),我还会粘贴这个结果:
mysql> select count(*) from m where fld is null;
+----------+
| count(*) |
+----------+
| 0 |
+----------+
1 row in set (0.00 sec)
编辑:为了回复评论,我也在编辑以下信息,以防有人回答我的问题:
select count(*) from m join i using (fld)
产生 9350; left join
, 73087; right join
, 28872.
select count(*) from i where fld is null
产量 810.
我现在明白了。 The documentation 读取:
To comply with the SQL standard, IN
returns NULL
not only if the expression on the left hand side is NULL
, but also if no match is found in the list and one of the expressions in the list is NULL
.
因此,只要在派生的 table(这里有)并且在派生的 table 中找不到 fld
(这是我正在测试的内容)。
可能有比这更好的解决方法,但我使用的是 (select fld from i where fld is not null)
。
mysql> show columns in m like 'fld';
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| fld | varchar(45) | YES | MUL | NULL | |
+-------+-------------+------+-----+---------+-------+
1 row in set (0.02 sec)
mysql> show columns in i like 'fld';
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| fld | varchar(45) | YES | MUL | NULL | |
+-------+-------------+------+-----+---------+-------+
1 row in set (0.02 sec)
mysql> select count(distinct fld) from i;
+---------------------+
| count(distinct fld) |
+---------------------+
| 27988 |
+---------------------+
1 row in set (0.03 sec)
mysql> select count(distinct fld) from m;
+---------------------+
| count(distinct fld) |
+---------------------+
| 72558 |
+---------------------+
1 row in set (0.07 sec)
根据我对相关表格的了解,上述结果似乎是合理的。
mysql> select count(*) from m where fld not in (select fld from i);
+----------+
| count(*) |
+----------+
| 0 |
+----------+
1 row in set (0.11 sec)
最后那个结果好像不太合理。 There must be 行在 m
中 fld
不在 i
中!有人可以解释为什么我得到 0
作为结果吗?
为了完整性(因为我怀疑它可能是相关的),我还会粘贴这个结果:
mysql> select count(*) from m where fld is null;
+----------+
| count(*) |
+----------+
| 0 |
+----------+
1 row in set (0.00 sec)
编辑:为了回复评论,我也在编辑以下信息,以防有人回答我的问题:
select count(*) from m join i using (fld)
产生 9350;left join
, 73087;right join
, 28872.select count(*) from i where fld is null
产量 810.
我现在明白了。 The documentation 读取:
To comply with the SQL standard,
IN
returnsNULL
not only if the expression on the left hand side isNULL
, but also if no match is found in the list and one of the expressions in the list isNULL
.
因此,只要在派生的 table(这里有)并且在派生的 table 中找不到 fld
(这是我正在测试的内容)。
可能有比这更好的解决方法,但我使用的是 (select fld from i where fld is not null)
。