I am trying to fetch records using different aliases but they are however related.
The server I’m using is WAMP. I have successfully connected my project/script to the PhpMyAdmin database and some records can be seen on my PHP file using some queries and be successfully inserted.
However, when I try to fetch records using different aliases I get an error stating the following: “MySQL said: #1066 – Not Unique table/alias:’teams'”?
Can you please explain whats going on because on the database when I check the ID’s of these records, they show that they are connected/pulling the record from another table.
The following is my MySQL query in which I tried to alter several times but no success came about after every attempt:
SELECT DISTINCT fixtures.fixture_id, fixtures.fixture_date,
fixtures.fixture_time, fixtures.home_teamID,
fixtures.away_teamID, fixtures.comp_id
From fixtures
JOIN teams ON fixtures.home_teamID = teams.team_id
JOIN teams ON fixtures.away_teamID = teams.team_id
JOIN competitions ON fixtures.comp_id = competitions.comp_id
ORDER BY fixture.id ASC
The Error Message from the query states that:
SELECT DISTINCT fixtures.fixture_id, fixtures.fixture_date,
fixtures.fixture_time, fixtures.home_teamID,
fixtures.away_teamID, fixtures.comp_id
From fixtures
JOIN teams ON fixtures.home_teamID = teams.team_id
JOIN teams ON fixtures.away_teamID = teams.team_id
JOIN competitions ON fixtures.comp_id = competitions.comp_id
ORDER BY fixture.id ASC
LIMIT 0, 25
Please explain to me what am I doing wrong and please note I am new to this platform using PHP and MySQL. Thanks in advance.
2
Answers
If you want join two ime the same table you need alias
in this sample a and b are the alias for the same table used in two way one for home_team and one for way_team
and for a more compact code as suggested by @Tim Biegeleisen you can use alias as
You’re joining ‘teams’ two times, so when you refer to ‘teams’ it doesn’t know whether you mean the first teams that was joined on home_teamID or the second one that was joined on away_teamID.
You fix this by giving each join a different name like so: