我怎樣才能看到什麼不在表中......我知道我知道......只能看到有什麼,但來吧!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