如何更正此 sql 连接上的相关名称?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8956577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I correct the correlation names on this sql join?
提问by Logical Fallacy
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.
我需要一个从两个不同表中产生三个同名字段的连接。当我尝试运行我的 sql 查询时,VS 给了我以下错误。
The objects "PoliticalFigures" and "PoliticalFigures" in the FROM clause have the same exposed names. Use correlation names to distinguish them.
FROM 子句中的对象“PoliticalFigures”和“PoliticalFigures”具有相同的公开名称。使用相关名称来区分它们。
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:
我一直在尝试使用“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
If it's not obvious from the code, these are the tables.
如果从代码中看不出来,这些就是表格。
- Countries: Id, Name, President_Id, VicePresident_Id.
- PoliticalFigures: Id, Name.
- Joined table: Country, President, VicePresident
- 国家/地区:Id、名称、President_Id、VicePresident_Id。
- 人物:身,姓名。
- 加入表:国家、总统、副总统
(Note, the tables and fields in my application have different names. I am generalizing them to make this example clearer and hopefullymore relevant to others.)
(请注意,我的应用程序中的表和字段有不同的名称。我将它们概括起来以使这个示例更清晰,并希望与其他人更相关。)
(The tools I'm using are Visual Web Developer 2010 Express and SQL Server 2008 Express.)
(我使用的工具是 Visual Web Developer 2010 Express 和 SQL Server 2008 Express。)
回答by Michael Berkowski
Use table aliases for each reference to PoliticalFigures
instead:
为每个引用使用表别名PoliticalFigures
:
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
回答by Bohemian
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
回答by onedaywhen
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.
在 SQL 标准中,白话“表别名”称为 a correlation name
,白话“列别名”称为as clause
。看来您将这两个术语混淆了。
回答by Luis
You need to use AS on the tables to give them aliases:
您需要在表上使用 AS 为它们提供别名:
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