So here's my problem. I have three tables:
Person Table
person_id, name
Events Table
event_id, event_name, event_date
Matches Table
match_id, person_a, person_b, winner, event
So in the matches table, person_a, person_b, and winner are integers that correspond to person_id in the Person table.
If I pull in a row from Matches table, it would come out like this:
1, 1, 2, 1, Chess Tournament #33
How can I lookup those 3 fields (person_a, person_b, and winner) with the Person table to pull in their actual names so that the result is?
1, John, Steven, John, Chess Tournament #33
I looked into joining the two tables, but it doesn't makes sense because I can't just do ON id=id, or WHERE id=id. I appreciate any help.
SELECT
Matches.match_id AS match_id,
PersA.name AS persa_name,
PersB.name AS persb_name,
IF(winner=person_a,PersA.name,PersB.name) AS winner_name,
Events.event_name AS event_name
FROM
Matches
INNER JOIN Person AS PersA ON Matches.person_a=PersA.person_id
INNER JOIN Person AS PersB ON Matches.person_b=PersB.person_id
INNER JOIN Events ON Matches.event=Events.event_id
WHERE
-- whatever