1

好的,我有一张桌子,我有一个包含数千条记录的 Id 列......我还有另一个像这样的逗号分隔的 Id 列表

1, 2, 457, 558, 998

我想检查这个 people 表,看看 5 条记录中的哪条不存在......

我试过

select id from people where id not in (1, 2, 457, 558, 998)

但这会返回所有其他一千条记录,而不仅仅是这 5 条中未找到的记录

任何想法我错过了什么

4

3 回答 3

2
select a.id
from (
    select 1 as id
    union all
    select 2
    union all
    select 457
    union all
    select 558
    union all
    select 998
) a 
left outer join people p on a.id = p.id
where p.id is null

如果您要检查的值在表中,您可以这样做:

select c.id
from MyCheckValues c
left outer join people p on c.id = p.id
where p.id is null
于 2012-09-13T18:45:26.410 回答
1

也许是这样的:

Select id from people where id in (1, 2, 457, 558, 998)
and id not in ( select id from people )
于 2012-09-13T18:45:47.677 回答
0
SELECT id FROM people WHERE id IN (1, 2, 457, 558, 998)

如果您有多个“id”,则使用:

SELECT DISTINCT(id) FROM people WHERE id IN (1, 2, 457, 558, 998)
于 2012-09-13T18:48:47.863 回答