I have a database setup in MySQL, with two different tables:
Table "matches"
ID TEAM1ID TEAM2ID TIME DATE
-------------------------------------
5 13 14 21:00 5 May
5 4 7 22:00 7 May
Table "teams"
ID NAME TEAMLEADER
-----------------------------------
13 GucciGang Peter
4 Beatles Peter
14 Beachboys Peter
7 BackstreetBoys Peter
I want to be able to assosicate the IDs in table "matches" with the team names from table "teams", so I can print it out on a matchpage.
My problem is that I can't the query to suit my needs.
My latest attempt:
SELECT matches.*, teams.name AS team1name
FROM matches
LEFT JOIN teams ON matches.team1ID = teams.id
UNION
SELECT matches.*, teams.name AS team2name
FROM matches
LEFT JOIN teams ON matches.team2ID = teams.id
This is obviously not ideal, as I get all the match data twice.
This gives me both names, but they are on seperate rows and they are both labeled team1name.
The result I'm looking for is getting something like this:
ID TEAM1ID TEAM2ID TIME DATE TEAM1NAME TEAM2NAME
---------------------------------------------------------
5 13 14 21:00 5 May GucciGang BeachBoys
Is this possible?