簡體   English   中英

如何從SQL的表中選擇,其中一列中的元素僅包含另一列的特征,而另一列本身又包含兩個特征?

[英]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

有沒有辦法列出所有只有女性的球隊?

您可以使用joingroup byhaving

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM