45

此查询 (InvoiceID) 出现不明确的列名错误。我不知道为什么。它们似乎都已正确连接,那么为什么 SSMS 不知道显示 VendorID?

询问:

SELECT 
    VendorName, 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, InvoiceID, InvoiceSequence, InvoiceLineItemAmount
4

9 回答 9

62

当我们通过连接表从多个表中选择数据并且至少一个选定的列(使用 * 选择所有列时也会发生)在多个表中存在同名时,我们会遇到此错误(我们选择的/加入的表)。在这种情况下,我们必须指定从哪个表中选择列。

以下是上述概念的示例解决方案实现

我认为您只有在InvoiceID存在于InvoiceLineItemsInvoices其他领域似乎不同时才有歧义。所以试试这个

我只是用 Invoices.InvoiceID 替换 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

您可以在不使用任何别名的情况下对所有列(在选择、位置、分组依据和排序依据)使用 tablename.columnnae。但是,您可以按照其他答案的指导使用别名

于 2012-09-30T16:45:36.570 回答
9

InvoiceID您在Invoices表格和表格中都有一列InvoiceLineItems。查询执行引擎无法知道您想要返回哪一个。

添加表别名将有助于:

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
于 2012-09-30T16:38:36.840 回答
6

因为您要连接两个表 Invoices 和 InvoiceLineItems,它们都包含 InvoiceID。更改为 Invoices.InvoiceID 以使其正确。

于 2012-09-30T16:40:49.673 回答
4

很可能两个表都有同名的列。给每个表起别名,并用表别名调用每一列。

于 2012-09-30T16:39:05.250 回答
4

这是因为两个表上都存在某些字段(特别是Invoices 表和 InvoiceLineItems 上的InvoiceID ) 。回答问题的方法是在其上添加一个。ALIAS

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
于 2012-09-30T16:39:48.687 回答
4

如果您连接 2 个或更多表并且它们的列具有相似的名称,SQL 服务器希望您限定它们所属的列。

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

如果 Events 和 Units 表具有相同的列名 (ID) SQL 服务器希望您使用别名。

于 2015-08-26T07:50:34.710 回答
1

它不仅仅发生在带有连接的查询中。

当您在单个表查询上使用 ORDER BY 且列名在查询中出现两次时,可能会发生这种情况。

例如。

SELECT firstname, * FROM person ORDER BY firstname;`

因为firstname在结果集中出现了两次,所以您要按哪个排序是不明确的(即使它们都是相同的)。

您可以使用这些别名中的任何一个来解决它


SELECT firstname AS fn, * FROM person ORDER BY firstname;

SELECT p.firstname, * FROM person p ORDER BY firstname;

-- Its not really clear to me why this next one works but it does
SELECT p.firstname, p.* FROM person p ORDER BY p.firstname;

于 2021-08-27T00:40:06.497 回答
0

您的一个表具有相同的列名,这会在查询中导致您所指表的哪些列的混淆。复制此代码并运行它。

SELECT 
    v.VendorName, i.InvoiceID, iL.InvoiceSequence, iL.InvoiceLineItemAmount
FROM Vendors AS v
JOIN Invoices AS i ON (v.VendorID = .VendorID)
JOIN InvoiceLineItems AS iL ON (i.InvoiceID = iL.InvoiceID)
WHERE  
    I.InvoiceID IN
        (SELECT iL.InvoiceSequence 
         FROM InvoiceLineItems
         WHERE iL.InvoiceSequence > 1)
ORDER BY 
    V.VendorName, i.InvoiceID, iL.InvoiceSequence, iL.InvoiceLineItemAmount
于 2018-11-29T10:01:15.447 回答
0

发生这种情况是因为在多个表中存在具有相同名称的字段,在查询中,由于连接,因此您应该以不同的方式引用字段,为表提供名称(别名)。

于 2018-12-19T11:18:51.903 回答