简体   繁体   English

mysql连接三个表以分组显示

[英]mysql join three tables to display as group by

I must to joing three tables but I'm not so good at this. 我必须参加三桌比赛,但是我不太擅长此事。 I have three tables 我有三张桌子

tournaments

id | name |

teams 

id | name

teams_to_tournament

id | tournament_id | team_id

now i want to join this into one query so I can display all tournaments and below teams for that tournaments 现在我想将其加入一个查询,以便我可以显示所有锦标赛以及该锦标赛的以下队伍

Tournament name 
tournament team1,tournament team 3,...

Tournament name 2
tournament team1,tournament team 3,...

please can you help me with this. 请你能帮我这个忙。

You need to use GROUP_CONCAT function with GROUP BY tournament.name . 您需要将GROUP_CONCAT函数与GROUP BY tournament.name GROUP_CONCAT一起使用。

  SELECT tr.name AS tournament, GROUP_CONCAT (tm.name) AS teams
    FROM tournaments tr JOIN teams_to_tournaments tt
         ON tr.tournament_id = tt.tournament_id
         JOIN teams tm ON tm.team_id = tt.team_id
GROUP BY tr.name;

Read more about GROUP_CONCAT here . 在此处阅读有关GROUP_CONCAT更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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