[英]SELECT from a table where a a column contains only elements from another column
[英]How to select from a table in SQL where element in a column contains only one feature of another column which in turn contains two features in itself?
可以说我在SQL中有一个table1,如下所示。
Team PlayerID
Team1 12
Team2 56
Team1 78
Team1 96
Team3 23
Team2 45
Team3 89
Team3 78
现在我有一个新的table2作为
PlayerID Gender
12 Male
56 Female
78 Female
96 Male
23 Female
45 Male
89 Female
78 Female
有没有办法列出所有只有女性的球队?
您可以使用join
, group by
和having
:
select t.team
from teams t join
players p
on p.playerid = t.playerid
group by t.team
having min(gender) = max(gender) and min(gender) = 'Female';
如果您确定每队至少有1名球员,那么这应该可行:
SELECT T1.Team
FROM Table1 T1
LEFT JOIN Table2 T2 ON T1.PlayerID = T2.PlayerID AND t2.Gender = 'Male'
GROUP BY T1.Team
Having COUNT(Gender) = 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.