![](/img/trans.png)
[英]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.