[英]I do not understand why I am getting the error message Ambiguous column name 'FirstName' when compiling
SELECT FirstName
FROM Adjuncts
INNER JOIN Faculty ON Adjuncts.FirstName = Faculty.FirstName
This is my Microsoft SQL Server 2012 code.这是我的 Microsoft SQL Server 2012 代码。 I am trying to join two tables together (
Adjuncts
and Faculty
) and share their FirstName
, and LastName
columns.我正在尝试将两个表连接在一起(
Adjuncts
和Faculty
)并共享它们的FirstName
和LastName
列。 I tried to just do one ( FirstName
) to see if I could make it work, and could not.我试着只做一个(
FirstName
)来看看我是否可以让它工作,但不能。 I was receiving the following error message:我收到以下错误消息:
Msg 209, Level 16, State 1, Line 1
Msg 209, Level 16, State 1, Line 1
Ambiguous column name 'FirstName'不明确的列名“名字”
Is anyone able to tell me how to properly join the two tables together?有人能告诉我如何正确地将两个表连接在一起吗? And how to show ONLY the 'FirstName' & 'LastName' columns which are defined in each of the tables WITHOUT overlapping the 'FirstName' & 'LastName' columns and having them twice.
以及如何仅显示在每个表中定义的 'FirstName' 和 'LastName' 列,而不与 'FirstName' 和 'LastName' 列重叠并且有两次。
Both tables have FirstName
in them -- as clearly shown by the ON
clause.两个表中都有
FirstName
——正如ON
子句清楚地显示的那样。
I would recommend that you use table aliases for your qualified column names:我建议您为限定的列名使用表别名:
SELECT a.FirstName
FROM Adjuncts a INNER JOIN
Faculty f
ON a.FirstName = f.FirstName;
Both tables in your query ( Adjuncts
and Faculty
) have a FirstName
column, so you have to fully qualify it.查询中的两个表(
Adjuncts
和Faculty
)都有一个FirstName
列,因此您必须完全限定它。 Eg:例如:
SELECT Adjuncts.FirstName
-- Here ---^
FROM Adjuncts
INNER JOIN Faculty ON Adjuncts.FirstName = Faculty.FirstName
Try using the following query, this might have a match so there will be data presented.尝试使用以下查询,这可能有匹配项,因此将显示数据。 It is really difficult to have a match when you are not using key columns in the
ON
clause of JOIN
当您在
JOIN
的ON
子句中不使用键列时,很难进行匹配
SELECT a.firstname
FROM adjuncts a
JOIN faculty f
ON UPPER(a.firstname) = UPPER(f.firstname)
Also, do not add f.firstname in the SELECT
clause if you do not want to have two different columns for firstname.此外,如果您不希望 firstname 有两个不同的列,请不要在
SELECT
子句中添加 f.firstname。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.