I need a join that yields three fields with the same name from two different tables. When I try to run my sql query, VS gives me the following error.
Use table aliases for each reference to PoliticalFigures
instead:
SELECT
Countries.Name AS Country,
P.Name AS President,
VP.Name AS VicePresident
FROM
Countries
LEFT OUTER JOIN PoliticalFigures AS P ON Countries.President_Id = P.Id
LEFT OUTER JOIN PoliticalFigures AS VP ON Countries.VicePresident_Id = VP.Id
Give each reference to the table an alias:
SELECT
Countries.Name AS Country,
P.Name AS President,
VP.Name AS VicePresident
FROM Countries
LEFT JOIN PoliticalFigures P ON Countries.President_Id = P.Id
LEFT JOIN PoliticalFigures VP ON Countries.VicePresident_Id = VP.Id
In the SQL Standards, the vernacular 'table alias' is referred to as a correlation name
and the vernacular 'column alias' is referred to as an as clause
. It seems you have the two terms confused.
You need to use AS on the tables to give them aliases:
SELECT Countries.Name AS Country, Pres.Name AS President, Vice.Name AS VicePresident FROM Countries
LEFT OUTER JOIN PoliticalFigures AS Pres ON Countries.President_Id = Pres.Id
LEFT OUTER JOIN PoliticalFigures AS Vice ON Countries.VicePresident_Id = Vice.Id