[英]SQL Statement using Where clause with multiple values
我有一個包含多行的表,其中包含以下字段:
PersonName SongName Status
我想使用從多選列表框中選擇的名稱,我可以從中檢索值,然后執行一個 where 子句,以便它顯示所選人員都可以播放的歌曲名稱,因此狀態是完整的。
例如:
PersonName SongName Status
Holly Highland Complete
Holly Mech Complete
Ryan Highland Complete
如果我從列表框中輸入 select Holly 和 Ryan 並按下按鈕,查詢應該只顯示 Highland,因為這是他們都知道的。
試試這個:
select songName from t
where personName in ('Ryan', 'Holly')
group by songName
having count(distinct personName) = 2
人數應與人數相符。 如果您還需要 Status 為Complete
,請使用此where
子句而不是前一個:
where personName in ('Ryan', 'Holly') and status = 'Complete'
SELECT PersonName, songName, status
FROM table
WHERE name IN ('Holly', 'Ryan')
如果您使用的是參數化存儲過程:
INNER JOIN ON t.PersonName = newTable.PersonName
使用包含傳入名稱的表變量Select t1.SongName
From tablename t1
left join tablename t2
on t1.SongName = t2.SongName
and t1.PersonName <> t2.PersonName
and t1.Status = 'Complete' -- my assumption that this is necessary
and t2.Status = 'Complete' -- my assumption that this is necessary
and t1.PersonName IN ('Holly', 'Ryan')
and t2.PersonName IN ('Holly', 'Ryan')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.