mysql:使用join代替subquery

lstz6jyr  于 2021-06-25  发布在  Mysql
关注(0)|答案(1)|浏览(334)

这个查询工作得很好,但是如何使用join而不是subquery呢?

SELECT  
    games.id, 
    games.team_home,
    games.score_home,
    games.team_away,
    games.score_away,
    (SELECT teams.name FROM teams WHERE teams.id=games.team_home ) as t1,
    (SELECT teams.name FROM teams WHERE teams.id=games.team_away ) as t2
FROM 
    games, teams 
WHERE  
    games.date_game = '2018-06-15'  
GROUP BY
    games.id
wi3ka0sx

wi3ka0sx1#

您可以这样做,id上有两个连接:

SELECT games.id,
       games.team_home,
       games.score_home,
       games.team_away,
       games.score_away,
       t1.name,
       t2.name
FROM games join teams t1 on teams.id=games.team_home 
           join teams t2 on teams.id=games.team_away 
WHERE games.date_game='2018-06-15'  
group by games.id

相关问题