본문 바로가기
MySql

MySQL Double Join

by 베이스 공부 2021. 2. 2.
반응형

두 개의 테이블이 있습니다.

teams
----------------
|uid|name |rank|
----------------
|  1  |Team1|  1  |
|  2  |Team2|  2  |
----------------

games
-----------------------------------------------------------------------
|uid|team_one_uid|team_one_score|team_two_uid|team_two_score|game_date|
-----------------------------------------------------------------------
|1|1|70|2|50|2012-12-12|

팀 테이블에는 팀 목록과 순위와 같은 기타 데이터가 있습니다. The games table has a list of games and references each team by it's unique id (uid). 다음 열이있는 행이 포함 된 결과를 보려면 어떤 쿼리를 실행할 수 있습니까?

game_uid, team_one_name, team_one_rank, team_one_score, team_two_name, team_two_rank, team_two_score, game_date

 

해결 방법

 

select g.uid as game_uid, 
       t1.name as team_one_name, 
       t1.rank as team_one_rank, 
       team_one_score, 
       t2.name as team_two_name, 
       t2.rank as team_two_rank, 
       team_two_score, 
       game_date
from games g 
inner join teams t1 on t1.uid = team_one_uid
inner join teams t2 on t2.uid = team_two_uid

 

참조 페이지 https://stackoverflow.com/questions/11853413

 

 

반응형

댓글