Query error with ambiguous column name in SQL

前端 未结 8 721
被撕碎了的回忆
被撕碎了的回忆 2020-11-27 06:54

I get an Ambiguous column name error with this query (InvoiceID). I can\'t figure out why. They all seem to be joined correctly so why doesn\'t the management studio know to

相关标签:
8条回答
  • 2020-11-27 07:33

    I think you have ambiguity only in InvoiceID. Other fields seem distinct. So try This

    I just replace InvoiceID with Invoices.InvoiceID

       SELECT 
            VendorName, Invoices.InvoiceID, InvoiceSequence, InvoiceLineItemAmount
        FROM Vendors 
        JOIN Invoices ON (Vendors.VendorID = Invoices.VendorID)
        JOIN InvoiceLineItems ON (Invoices.InvoiceID = InvoiceLineItems.InvoiceID)
        WHERE  
            Invoices.InvoiceID IN
                (SELECT InvoiceSequence 
                 FROM InvoiceLineItems
                 WHERE InvoiceSequence > 1)
        ORDER BY 
            VendorName, Invoices.InvoiceID, InvoiceSequence, InvoiceLineItemAmount
    

    You can use tablename.columnnae for all columns (in selection,where,group by and order by) without using any alias. However you can use an alias as guided by other answers

    0 讨论(0)
  • 2020-11-27 07:34

    it's because some of the fields (specifically InvoiceID on the Invoices table and on the InvoiceLineItems) are present on both table. The way to answer of question is to add an ALIAS on it.

    SELECT 
        a.VendorName,  Invoices.InvoiceID, .. -- or use full tableName
    FROM Vendors a   -- This is an `ALIAS` of table Vendors
    JOIN Invoices ON (Vendors.VendorID = Invoices.VendorID)
    JOIN InvoiceLineItems ON (Invoices.InvoiceID = InvoiceLineItems.InvoiceID)
    WHERE  
        Invoices.InvoiceID IN
            (SELECT InvoiceSequence 
             FROM InvoiceLineItems
             WHERE InvoiceSequence > 1)
    ORDER BY 
        VendorName, InvoiceID, InvoiceSequence, InvoiceLineItemAmount
    
    0 讨论(0)
  • 2020-11-27 07:41

    Most likely both tables have a column with the same name. Alias each table, and call each column with the table alias.

    0 讨论(0)
  • 2020-11-27 07:43

    You have a column InvoiceID in the Invoices table and also in the InvoiceLineItems table. There is no way for the query execution engine to know which one you want returned.

    Adding a table alias will help:

    SELECT V.VendorName, I.InvoiceID, IL.InvoiceSequence, IL.InvoiceLineItemAmount
    FROM Vendors V
    JOIN Invoices I ON (...)
    JOIN InvoiceLineItems IL ON (...)
    WHERE ...
    ORDER BY V.VendorName, I.InvoiceID, IL.InvoiceSequence, IL.InvoiceLineItemAmount
    
    0 讨论(0)
  • 2020-11-27 07:47

    if you join 2 or more tables and they have similar names for their columns sql server wants you to qualify columns which they belong.

    SELECT  ev.[ID]
        ,[Description]
        FROM   [Events] as ev 
        LEFT JOIN  [Units] as un ON ev.UnitID = un.UnitId  
    

    if Events and Units tables has same column name (ID) SQL server wants you to use aliases.

    0 讨论(0)
  • 2020-11-27 07:53

    Because you are joining two tables Invoices and InvoiceLineItems that both contain InvoiceID. change to Invoices.InvoiceID to make it correct.

    0 讨论(0)
提交回复
热议问题