Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

need help writing a sql query with multiple join conditions

i have a sql query currently with a multiple inner join

select game_id, start_time_utc, t.name from games g
inner join teams t
on g.home_team_id = t.team_id 
or g.away_team_id = t.team_id

the problem is i want t.name for both the home_team_id and the away_team_id however it is just giving the first instance with his home_team_id
how do i modify the sql query so i get back both the home team name and the away team name

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You need to join the table twice.

select game_id, start_time_utc, t.name as home_team_name, t2.name as away_team_name 
from games g
inner join teams t on g.home_team_id = t.team_id 
inner join teams t2 on g.away_team_id = t2.team_id
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading