我正在尝试创建一个 SQL 查询,该查询将返回至少有 2 个粉丝的非队长球员的姓名。
我的难点在于,球迷被分成了最喜欢的球队和最喜欢的球员,所以我必须把最喜欢的球员作为一个团队和个人来计算。
这是我想出的,但它没有返回正确的记录:
select players.name
from players
-- join relevant relations
join teams
on teams.name = players.team
join favplayers
on players.name = favplayers.player
join favteams
on players.team = favteams.team
-- conditions
where players.team <> teams.captain
group by players.name
having (count(favplayers.player) + count(favteams.team)) > 1;
以下是供参考的关系模型:
有任何想法吗?
输出:
NAME COUNT(DISTINCTFAVPLAYERS.FAN) COUNT(DISTINCTFAVTEAMS.FAN)
-------------------- ----------------------------- ---------------------------
Arthurs 3 1
Becker 1 1
Bryan 0 3
Greul 0 2