简体   繁体   中英

Mysql : using Join instead of subQuery

This query work fine but how i can use join instead of 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

You can do it this way, with two JOINS on the 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

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