简体   繁体   English

我不明白为什么我在编译时收到错误消息 Ambiguous column name 'FirstName'

[英]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.我正在尝试将两个表连接在一起( AdjunctsFaculty )并共享它们的FirstNameLastName列。 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.查询中的两个表( AdjunctsFaculty )都有一个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当您在JOINON子句中不使用键列时,很难进行匹配

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.

相关问题 为什么在尝试执行此 SQL 语句时会收到 Ambiguous Column name CountryID 错误消息: - Why am I Getting an Ambiguous Column name CountryID error message when trying to execute this SQL statement: 为什么在此查询中得到“歧义列名”? - Why am I getting “Ambiguous column name” in this query? 为什么我在查询中收到错误:“歧义列”? - Why am I getting the error: "Ambiguous column" in my query? 我正在尝试做一个连接表,但我得到了不明确的列错误 - I am trying to do a join table and I am getting ambiguous column error 列存在时为什么出现“无效的列名”错误 - Why am I getting “Invalid column name” error when the column exists 为什么我在分组依据中收到无效的列名错误? - Why am I getting invalid column name error in group by? 为什么在使用isNull时收到此错误消息 - Why am I getting this error message when using isNull 在当前批次中声明变量后,为什么会收到错误消息“ Invalid Object Name”? - Why am I getting an error message that says “Invalid Object Name” when I've declared my variable in the current batch? 为什么在 PostgreSQL 中创建生成的列时出现错误? - Why am I getting a an error when creating a generated column in PostgreSQL? 不明确的列名错误,我该如何解决? - Ambiguous column name error, how do I fix it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM