我有兩個表:
表PL:
| 折疊 | 名稱 |
|---|---|
| 1 | 愛麗絲 |
| 2 | 約翰 |
| 3 | 丹妮爾 |
和表PLproducts
| 折疊 | 產品識別符號 |
|---|---|
| 1 | 會員資格 |
| 1 | 生活 |
| 1 | 牙科 |
| 2 | 會員資格 |
| 3 | 會員資格 |
| 3 | 生活 |
| 3 | 汽車 |
我需要找到那些 productIdentifier 不包含“dental”的 plid
預期成績:
| 折疊 | 名稱 |
|---|---|
| 2 | 約翰 |
| 3 | 丹妮爾 |
如果我為 PLproducts <> 'dental' 進行外部連接,我會得到所有不包含 'dental' 的記錄,但這不是我要找的。
我以前從未發現過這種情況。我明白這可能是一個簡單的問題。
謝謝你們。
uj5u.com熱心網友回復:
你正在尋找不存在的東西
select *
from pl
where not exists (
select * from plProducts p
where p.plid = pl.plid and p.productidentifier = 'dental'
);
uj5u.com熱心網友回復:
有多種方法可以解決這個問題。您可能有興趣查看outer apply. 在更復雜的場景中,這可能是一種有用的方法。
select pl.*
from pl outer apply (
select count(*) as hasdental from plproducts pp
where p2.plid = pl.plid and pl.productidentifier = 'dental'
) as oa
where hasdental = 0;
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/349925.html
標籤:sql sql-server
上一篇:計陣列合 頻率
