我很难弄清楚所有人的名字,也不知道他们是否会打橄榄球。
如果播放,则显示Yes,否则为No。目前我只得到一个人打橄榄球的结果,而不是其他不打橄榄球的人。有谁可以帮我?
当前SQL:
select P1.name, case when S.sport = 'rugby' then 'Yes' else 'No' end as rugby
from Persons P1,
Persons P2,
SportTogether S
where P1.id = S.personA_id
and P2.id = S.personB_id
and S.sport = 'rugby'
group by case when S.sport = 'rugby' then 'Yes' else 'No' end;发布于 2022-05-24 12:40:27
在我看来,你使用了错误的方法。您想要选择人员,所以从persons表中选择。你想知道一个人是否玩橄榄球,所以在橄榄球桌上找人。查找可以用IN或EXISTS完成。
select
name,
case when exists
(
select null
from sporttogether s
where s.sport = 'rugby'
and p.id in (s.persona_id, s.personb_id)
) then 'Yes' else 'No' end as plays_rugby
from persons p
order by name;https://stackoverflow.com/questions/72362824
复制相似问题