简体   繁体   中英

Get rows that have common value from the same table with different id

I just reviewed this question but what I need is something quite different.

However, I have this table:

team_players_order 
    (team_id, 
    player_id, 
    ordering, 
    captain
    )

The table stores a list of teams ids along with their players ids that belong to each team (each team could get 0-15 players). What I want to get is the teams that have a common players among them.

The list of teams I want to compare could be known (I have their ids)or unknown, then I might need to search the whole table and compare all teams. .

Here's a sample data for three teams:

        team_id    player_id  ordering   captain    
        117        134        0          N          
        117        55         1          N          
        117        97         2          N          
        117        215        3          N          
        117        165        4          N          
        117        221        5          N          
        117        163        6          N          
        117        128        7          N     >> common player     
        117        180        8          N          
        117        96         9          N          
        117        162        10         N          
        117        88         11         N          
        117        229        12         N          
        117        91         13         N          
        117        105        14         N    
    -----------------------------------------------      
        124        88         0          N          
        124        165        1          N          
        124        92         2          N          
        124        130        3          N          
        124        47         4          N          
        124        221        5          N          
        124        30         6          N          
        124        223        7          N          
        124        105        8          Y          
        124        6          9          N          
        124        96         10         N          
        124        120        11         N          
        124        198        12         N          
        124        128        13         N          >> common player
        124        202        14         N  
-----------------------------------------------      
        125        256        0          N          
        125        58         1          N          
        125        10         2          N          
        125        47         3          N          
        125        103        4          N          
        125        167        5          N          
        125        221        6          N          
        125        128        7          N          >> common player
        125        105        8          N          
        125        96         9          Y          
        125        180        10         N          
        125        210        11         N          
        125        229        12         N          
        125        30         13         N          
        125        33         14         N       

As you can see, player 128 is common player among these three teams. I need to find other common players as well.

What I have tried so far is the following query which I guess is comparing each giving team with all other teams and get any common player that exists individually of each comparison.

SELECT
  t1.team_id,
  t1.player_id,
  t2.team_id,
  t2.player_id
FROM team_players_order AS t1
  INNER JOIN team_players_order AS t2
    ON (t1.team_id != t2.team_id
        AND t1.player_id = t2.player_id)
WHERE t1.team_id IN(117,124,125)
    AND t2.team_id IN(117,124,125)
ORDER BY t1.team_id, t2.team_id

which returns:

team_id    player_id  team_id    player_id  
117        221        124        221        
117        88         124        88         
117        96         124        96         
117        105        124        105        
117        128        124        128        
117        165        124        165        
117        180        125        180        
117        221        125        221        
117        229        125        229        
117        96         125        96         
117        105        125        105        
117        128        125        128        
124        128        117        128        
124        165        117        165        
124        221        117        221        
124        88         117        88         
124        96         117        96         
124        105        117        105        
124        128        125        128        
124        30         125        30         
124        221        125        221        
124        47         125        47         
124        96         125        96         
124        105        125        105        
125        128        117        128        
125        180        117        180        
125        221        117        221        
125        229        117        229        
125        96         117        96         
125        105        117        105        
125        128        124        128        
125        221        124        221        
125        30         124        30         
125        47         124        47         
125        96         124        96         
125        105        124        105        

But what I want is:

  1. the players that exist in all giving teams (by their ids)
  2. the player that exist in all teams.

nb the list of teams could reach to 100 once it's given.

HAVING is the solution

the player that exist in all teams

select team_id, player_id, count(*) nb from team_players_order
group by player_id 
having nb > 1

the players that exist in all giving teams

select team_id, player_id, count(*) nb from team_players_order
where team_id in (124, 117)
group by player_id 
having nb > 1

You can use "HAVING COUNT" in order to know which player is in multiple teams.

SELECT player_id FROM team_players GROUP BY team_id HAVING COUNT(team_id) > 1

And to list teams that have a player in other teams :

 SELECT team_id FROM team_players GROUP BY team_id HAVING COUNT(player_id) > 1

Hope that helps !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM