I have the following tables in a MySQL database:
team
team_id name
3 Rangers
12 Capitals
19 Red Wings
4 Bruins
212 Avalanche
102 Flyers
20 Islanders
50 Sabres
7 Stars
player
id name
2 Zach
1 Deb
17 William
9 Viktor
12 Andrew
41 Chris
22 Bobby
5 Phil
3 Roy
92 Li
6 Juan
players_in
team_id player_id points
3 2 42
212 2 19
3 12 18
19 12 2
3 41 2
4 41 1
212 41 78
212 17 1
19 41 4
12 41 2
3 17 6
4 1 9
102 1 40
102 22 7
20 22 19
20 5 22
50 3 20
12 92 15
12 17 8
7 6 12
Here is a SQL Fiddle with the data: http://www.sqlfiddle.com/#!9/989ebe/1
I would like to get the name
and id
of the players who have played on ALL of the teams that Zach has played on.
In this case, Zach has played for the Rangers and the Avalanche.
Therefore, the desired result set would be:
name id
William 17
Chris 41
(because these players were part of both the Rangers and the Avalanche teams)
How would I do this?
Thanks!
3
Answers
Using a
cte
for Zach’s games and then checking all potential memberships based onteam_id
existence in thecte
‘s values:See fiddle.
First of all I’ve joined players_in (pi) with players (p) obtaining the set of all players and theirs teams.
Second, cross joined player zack joined with player_in (pi2) obtaining the set of Zach’s teams. Joined pi2 with pi I’ve obtained the set of all player that had played in a Zach’s team.
Now the where conditions:
I’ve selected all Zach teams again (pi3) not in the set of the player’s (p) team,
SQL Fiddle here
Your requirement could be translated to: searching for players which there’s not exists any Jack’s team that they don’t play in. Corresponding query could be:
Demo: http://www.sqlfiddle.com/#!9/989ebe/61