gpt4 book ai didi

sql - SQL 中列名不明确的查询错误

转载 作者:行者123 更新时间:2023-12-03 09:16:22 25 4
gpt4 key购买 nike

此查询 (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

最佳答案

当我们从 中选择数据时,我们会遇到这个错误。多 table 通过连接表和 所选列中至少有一个 (使用 * 选择所有列时也会发生)在多个表中存在同名 (我们选择/连接的表)。在这种情况下,我们必须指定从哪个表中选择列。
以下是上述概念的示例解决方案实现
我觉得你只有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 用于所有列(in selection、where、group by 和 order by),而无需使用任何别名。但是,您可以按照其他答案的指导使用别名

关于sql - SQL 中列名不明确的查询错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12662954/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com