WHERE IN sql 查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15210886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WHERE IN sql query
提问by pirmas naujas
I need to find which items in WHERE IN clause do not exist in the database. in below example cc33 does not exist and I need the query to give back cc33. how would I do that ?
我需要找到数据库中不存在 WHERE IN 子句中的哪些项目。在下面的示例中,cc33 不存在,我需要查询来返回 cc33。我该怎么做?
SELECT id FROM tblList WHERE field1 IN ('aa11','bb22','cc33')
回答by Gordon Linoff
You need to put the values into a table rather than a list:
您需要将值放入表格而不是列表中:
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
回答by ypercube??
For SQl-Server versions of 2008+, you can use a Table Value Constructor:
对于 2008+ 的 SQl-Server 版本,您可以使用表值构造函数:
SELECT field1
FROM
( VALUES
('aa11'),('bb22'),('cc33')
) AS x (field1)
WHERE field1 NOT IN
( SELECT field1 FROM tblList ) ;
Tested at SQL-Fiddle
在SQL-Fiddle测试