1

可能重复: SQL:在获取 mysql 表中不存在的值
的表中查找缺少的 ID

只是想知道,是否有可能有一个查询以某种方式告诉您它在表中没有找到的值?

所以如果我有一个查询SELECT * FROM mytable WHERE id IN (1,2,3,4,5,6,7,8,9)并且只返回 2,3,6,7,9。我想知道没有找到1,4,5,8。

进行手动比较会有点困难,因为这将在一个表中超过 2,000 行以上运行(id 将通过一个 csv 文件提供,该文件可以复制到查询中)

提前致谢

4

4 回答 4

1

This is probably silly, but what about creating a temporary table containing all your IDs from which you'll substract the result of your SELECT query ?

于 2013-01-17T12:29:28.740 回答
1

未经测试,但在理论上:

Table 1:
+----+-----+
| id | num |
+----+-----+

Table 2:
+----+
| id |
+----+

表 1 包含您要查找的数据(并且num是包含任何数据的任何字段) 表 2 包含来自 CSV 的 ID

SQL:
SELECT COUNT(`Table1`.`num`) AS `count`
FROM `Table1`
LEFT JOIN `Table2` ON `Table1`.`id` = `Table2`.`id`
WHERE `count` = 0
于 2013-01-17T12:35:21.023 回答
0

关于什么:

SELECT *
   FROM (select 1 as f
               UNION
               SELECT 2 as f
               UNION
               SELECT 3 as f
               UNION
               SELECT 4 as f
               UNION
               SELECT 5 as f
               UNION
               SELECT 6 as f
               UNION
               SELECT 7 as f
               UNION
               SELECT 8 as f
               UNION
               SELECT 9 ) as s1
 WHERE f NOT IN (SELECT id FROM mytable);

于 2013-01-17T12:56:07.177 回答
0

快速解决方案,打开您的 csv 文件,将所有逗号替换为“union select”,将 select 放在该行的前面,并将其用作底部查询中查询的第一行。

所以 1,2,3 变成

选择 1 联合 选择 2 联合 选择 3

在下面的查询中使用它

    Select 1 union select 2 union select x -- replace this line with the line generated from your csv
    Except
    (
     Select id from mytable
    )
于 2013-01-17T12:44:53.457 回答