如何在MySQL的其他列中找到不存在的列值

时间:2016-03-09 21:00:50

标签: mysql

我有两个表应该在mId和keyValue列中存储相同的值。但是我运行以下命令,得到了以下结果:

mysql> select count(distinct mId) from Types;
+---------------------+
| count(distinct mId) |
+---------------------+
|             2098350 |
+---------------------+


select count(distinct keyValue) from EntityIndex;
 +--------------------------+
 | count(distinct keyValue) |
 +--------------------------+
 |                  2095481 |
 +--------------------------+

查找Type表中仅存在的mId。我运行以下命令,得到以下结果:

mysql> select distinct mId from Types where not exists 
   (select distinct keyValue from EntityIndex);
Empty set (0.00 sec)


mysql> select distinct keyValue from EntityIndex where not exists 
(select distinct mId from Types);
Empty set (0.01 sec)

问题是什么?

1 个答案:

答案 0 :(得分:1)

我认为你可以通过这种方式找到缺失的值

select a.mId, b.keyValue 
from Types as a
left join EntityIndex as b on a.mId = b.KeyValue 
where b.kvalue is null 

你也可以尝试不在

 select distinct mId from Types where mId not in
    (select distinct keyValue from EntityIndex);