我怎样才能看到什么不在表中......我知道我知道......只能看到有什么,但来吧!IN NOT IN SQL Server 2005
因此!!
select * from ORDER where State IN ('MA','PA','GA','NC')
所以我会得到MA和PA,但我希望看到GA和NC ....
NOT IN
将返回纽约州,新泽西州,CT ECT ....我只是想看看是什么在()
我怎样才能看到什么不在表中......我知道我知道......只能看到有什么,但来吧!IN NOT IN SQL Server 2005
因此!!
select * from ORDER where State IN ('MA','PA','GA','NC')
所以我会得到MA和PA,但我希望看到GA和NC ....
NOT IN
将返回纽约州,新泽西州,CT ECT ....我只是想看看是什么在()
看起来你遗失了GA前的单引号'
。
你只是试图找出哪些国家除了那四个?如果是这样的:
SELECT DISTINCT State FROM dbo.ORDER WHERE State NOT IN ('MA', 'PA', 'GA', 'NC')
我不认为这是个问题 – NullUserException 2010-10-12 20:06:52
@NullUserException - 虽然很难说! – JNK 2010-10-12 20:09:37
我这个问题的理解是:各国的给定的名单,其中没有在订单表中存在的吗?
这将显示出下面列出必须在Order
表中没有相应的记录四个什么规定:
select distinct s.State
from
(
select 'MA' as State
union all
select 'PA'
union all
select 'GA'
union all
select 'NC'
) s
left outer join [Order] o on s.State = o.State
where o.State is null
+1 - 这是对OP后面的一个很好的猜测。 – JNK 2010-10-12 20:16:58
我同意......这可能是最好的,尽管我会抛弃Distinct并使用个人偏好。 – 2010-10-12 20:40:33
我要去尝试一点这里的字里行间:
;with cteStates as (
select 'MA' as state
union all
select 'PA'
union all
select 'GA'
union all
select 'NC'
)
select s.state, count(o.state) as OrderCount
from cteStates s
left join [order] o
on s.state = o.state
group by s.state
这没有意义。您发布的查询将显示“STATE”与列表中的某个值匹配的所有记录。你将如何获得GA和NC?你可以发布你的表,查询和结果吗? – JNK 2010-10-12 20:04:40
如果你正在构造或执行查询,你不应该知道'()'中的内容吗?这到底是什么意思? – NullUserException 2010-10-12 20:05:31
顺便说一句:“ORDER”是一个SQL表格的可怕名称,因为它是一个关键字。“ – JohnFx 2010-10-12 20:07:07