How can I connect two tables in SQL depending on the data?

26 views Asked by At

It's quite simple problem but I am begginer and i have problems with it.

I have 2 tables in database

  1. matches id, team1id, team2id, score, date
  2. teams id, name

If I want to select team1 name, team2 name, score and date what command should I use?

1

There are 1 answers

1
Issac411 On

You should use a double "join" on teams table. I wrote a sheet of how you should use it :

select 
     matches.id,
     teamA.name,
     teamB.name,
     score,
     date 
from matches
inner join teams teamA
on teamA.id = matches.team1id
inner join teams teamB 
on teamB.id = matches.team2id