我有一个包含多行的表,其中包含以下字段:
PersonName SongName Status
我想使用从多选列表框中选择的名称,我可以检索这些值,然后执行where子句,以便显示所选人员可以播放的歌曲名称,因此状态已完成。
例如:
PersonName SongName Status
Holly Highland Complete
Holly Mech Complete
Ryan Highland Complete
如果我从列表框中选择Holly和Ryan并按下按钮,则查询应该只显示Highland,因为这是他们都知道的。
答案 0 :(得分:64)
试试这个:
select songName from t
where personName in ('Ryan', 'Holly')
group by songName
having count(distinct personName) = 2
该号码中的号码应与人数相匹配。如果您还需要状态为Complete
,请使用此where
子句而不是前一个:
where personName in ('Ryan', 'Holly') and status = 'Complete'
答案 1 :(得分:10)
SELECT PersonName, songName, status
FROM table
WHERE name IN ('Holly', 'Ryan')
如果您使用的是参数化存储过程:
INNER JOIN ON t.PersonName = newTable.PersonName
醇>
答案 2 :(得分:1)
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')