简体   繁体   中英

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. I am trying to join two tables together ( Adjuncts and Faculty ) and share their FirstName , and LastName columns. I tried to just do one ( FirstName ) to see if I could make it work, and could not. I was receiving the following error message:

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.

Both tables have FirstName in them -- as clearly shown by the ON clause.

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. 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM