I am trying to find an error in a massive SQL statement (not mine) – I have cut a lot of it out to make it readable – even pared down it still throws the error
SELECT DISTINCT Profiles.ID
FROM
(select * from Profiles RIGHT JOIN FriendList ON (FriendList.Profile = 15237)
order by LastLoggedIn DESC ) as Profiles
This returns an error
Duplicate column name ‘ID’
I have tested the the last part (select * from Profiles ... order by LastLoggedIn DESC) and it works fine by itself
I have tried to troubleshoot by changing column names in the DISTINCT section without any luck.
One solution I read was to remove the DISTINCT, but that didn’t help.
I just can’t see where the duplicate column error can be coming from. Could it be a database integrity problem?
Any help much appreciated.
Your
ProfileandFriendListtables both have anIDcolumn. Because you sayselect *, you’re getting two columns namedIDin the sub-select which is aliased toProfiles, and SQL doesn’t know which oneProfiles.IDrefers to (note thatProfileshere is referring to the alias of the sub-query, not the table of the same name).Since you only need the ID column, you can change it to this: