我需要一个连接,从两个不同的表中产生三个具有相同名称的字段.当我尝试运行我的SQL查询,VS给我以下错误.
The objects “PoliticalFigures” and “PoliticalFigures” in the FROM clause have the same exposed names. Use correlation names to distinguish them.
我一直在尝试使用“AS”来区分这些字段,但是我没有找到一个可行的解决方案.这是我正在运行的SQL查询:
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
如果从代码中不明显,这些是表.
>国家:Id,Name,President_Id,VicePresident_Id.
>政治图:Id,Name.
>参加表:国家,总统,副总统
(请注意,我的应用程序中的表和字段具有不同的名称,我将它们进行概括,以使此示例更清晰,并希望与其他人更相关.)
(我使用的工具是Visual Web Developer 2010 Express和sql Server 2008 Express.)
解决方法@H_404_22@
为每个参考政治图则使用表别名:
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
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