Query error with ambiguous column name in SQL

前端 未结 8 785
被撕碎了的回忆
被撕碎了的回忆 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

提交回复
热议问题