How can I correct the correlation names on this sql join?

前端 未结 4 1257
时光取名叫无心
时光取名叫无心 2020-12-03 06:58

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.

相关标签:
4条回答
  • 2020-12-03 07:20

    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
    
    0 讨论(0)
  • 2020-12-03 07:22

    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
    
    0 讨论(0)
  • 2020-12-03 07:33

    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.

    0 讨论(0)
  • 2020-12-03 07:34

    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
    
    0 讨论(0)
提交回复
热议问题