I have two tables, "teams" and "matches". I want to select and replace two team ID's from matches table with actual team names. I am able to get only one team name and I am not sure how I could select two.
SELECT m.*, t.teamId, t.teamName FROM matches AS m
JOIN teams AS t ON m.homeTeam = t.teamId
Matches Table
ID*
Date
homeTeam (id)
awayTeam (id)
Teams Table
ID*
Name
You need to join your teams
table twice. Once for each team entry in your matches
table. It should look something like this
SELECT
m.id
, m.date
, h.teamId as homeTeamID
, h.teamName as homeTeamName
, a.teamId as awayTeamID
, a.teamName as awayTeamName
FROM
matches AS m
JOIN teams AS h -- home team
ON m.homeTeam = h.teamId
JOIN teams AS a -- away team
on m.awayTeam = a.teamId
I've had to guess what you named the fields, but this should be enough to get you started.
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.