我需要找到WHERE IN子句中的哪些项目在数据库中不存在。在下面的示例中,cc33不存在,我需要查询以返回cc33。
我该怎么做?
SELECT id FROM tblList WHERE field1 IN ('aa11','bb22','cc33')
最佳答案
您需要将值放入表而不是列表中:
with list as (
select 'aa11' as val union all
select 'bb22' union all
select 'cc33'
)
select l.val
from list l left outer join
tbllist t
on l.val = t.field1
where t.field1 is null