0
SELECT VendorName, COUNT(*) as Total_Invoices
FROM   Invoices JOIN Vendors ON Invoices.VendorID = Vendors.VendorID
WHERE  Vendors.VendorName = 'IBM'
GROUP BY VendorName
ORDER BY Total_Invoices DESC

它会带来

VendorName  Total_Invoices
IBM                2

我想做,所以它只是说

Total_Invoices
     2

未显示 IBM VendorName 但仍计算 IBM 的总发票

4

3 回答 3

4

您可以安全地删除VendorName这种情况。

SELECT COUNT(*) as Total_Invoices
FROM   Invoices JOIN Vendors ON Invoices.VendorID = Vendors.VendorID
WHERE  Vendors.VendorName = 'IBM'
GROUP BY VendorName
ORDER BY Total_Invoices DESC

select另一方面,也称为投影。您可以安全地移除它而不会受到任何伤害。但是在GROUP BY子句中删除它已经是非常不同的事情了。

于 2012-11-15T13:55:04.567 回答
3

您只需VendorNameSelect. 在这种情况下你不需要使用GROUP BYorORDER BY

SELECT COUNT(*) as Total_Invoices
FROM   Invoices JOIN Vendors ON Invoices.VendorID = Vendors.VendorID
WHERE  Vendors.VendorName = 'IBM'
于 2012-11-15T13:55:12.973 回答
1

从 SELECT 中删除 VendorName。

于 2012-11-15T13:55:08.730 回答