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.
The objects "PoliticalFigures" and "PoliticalFigures" in the FROM clause have the same exposed names. Use correlation names to distinguish them.
I've been trying to use "AS" to distinguish these fields, but I haven't found a working solution. This is the sql query I'm running:
SELECT Countries.Name AS Country, PoliticalFigures.Name AS President, PoliticalFigures.Name AS VicePresident FROM Countries
LEFT OUTER JOIN PoliticalFigures ON Countries.President_Id = PoliticalFigures.Id
LEFT OUTER JOIN PoliticalFigures ON Countries.VicePresident_Id = PoliticalFigures.Id
If it's not obvious from the code, these are the tables.
(Note, the tables and fields in my application have different names. I am generalizing them to make this example clearer and hopefully more relevant to others.)
(The tools I'm using are Visual Web Developer 2010 Express and SQL Server 2008 Express.)
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