我正在嘗試進行嵌套查詢,我從多個列回傳值并將這些值與另一列中的值匹配。類似于下面的代碼(這是為了作業,所以我不能把確切的代碼放在這里)
select *
from store1
where (orderNum, customer, total) not in (
select orderNum, customer, total from store2)
當我嘗試運行代碼時,出現錯誤:
在預期條件的背景關系中指定的非布爾型別的運算式,靠近 ','。
有沒有辦法做到這一點是 SQL Server。我知道使用連接是一種選擇,但此時我更愿意避免這種情況。謝謝你的幫助!
uj5u.com熱心網友回復:
SQL Server 不支持這樣的表建構式,但有很多其他方法可以做到這一點。我的首選方法是NOT EXISTS:
SELECT * FROM dbo.store1
WHERE NOT EXISTS
(
SELECT 1 FROM dbo.store2
WHERE store1.orderNum = store2.orderNum
AND store1.customer = store2.customer
AND store1.total = store2.total
);
對于其他方法,請參閱:
- 我應該使用 NOT IN、OUTER APPLY、LEFT OUTER JOIN、EXCEPT 還是 NOT EXISTS?
uj5u.com熱心網友回復:
“count”回傳單個值,作業速度很快。容易明白。
select *
from store1
where
(select count(*) from store2 where store2.orderNum=store1.orderNum and store2.customer=store1.customer and store2.total=store1.total)=0
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/381369.html
標籤:sql sql-server 查询语句
