I am getting a bit confused with this basic SQL question, I was hoping you could give me a hint about what am I doing wrong:
I have a main table with different players:
id | name
----+-----
1 | John
2 | Paul
3 | Robert
And another table that stores their matches
date | player_home | player_away
----------+---------------+-------------
2012-03-21| 1 | 2
2012-04-10| 2 | 3
I am trying to build a query that outputs the names of both players for a given date, but the name of the same player is being repeated in both fields:
This retrieves nothing:
SELECT date, player_home, name, player_away, name
FROM games, players
WHERE date = (DATE '2012-03-21')
AND games.player_home = players.id
AND games.player_away = players.id;
And if I remove the second AND, it retrieves the same name (John) for both players:
date | player_home | name | player_away | name
----------+---------------+---------+----------------+-------
2012-03-21| 1 | John | 2 | John
When the name in the second "name" field should be "Paul".
What is the right way to build this query?
Thanks in advance