0

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

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

it works when i change it to : join teams t1 on t1.id=games.team_home join teams t2 on t2.id=games.team_away Thanks (Y)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.