如何在SQL查询中使用DISTINCT命令来显示供应商ID,公司名称以及在特定日期之前订购的该供应商的不同产品数量?我在Access中运行代码,但它没有有效地转换为SQL。表格出现了。
[Supplier ID Company Name Product Name Order Date
1 Exotic Liquids Chang 17-Aug-94
1 Exotic Liquids Chang 22-Nov-94
1 Exotic Liquids Aniseed Syrup 26-Sep-94]
我到目前为止的代码如下。我感到困惑的地方是放置DISTINCT语句的位置。它应该在Select之后立即吗?它除了SELECT之外还应该用括号吗?请原谅我对此主题缺乏了解。
SELECT Suppliers.SupplierID, Customers.CompanyName, Products.ProductName,
Orders.OrderDate
FROM Suppliers INNER JOIN
Products ON Suppliers.SupplierID = Products.SupplierID CROSS JOIN
Customers INNER JOIN
Orders ON Customers.CustomerID = Orders.CustomerID
WHERE Orders.OrderDate <='1/1/1999'
ORDER BY Suppliers.SupplierID
答案 0 :(得分:1)
我很确定就是这样:
SELECT DISTINCT(Suppliers.SupplierID), Customers.CompanyName, Products.ProductName,Orders.OrderDate
FROM Suppliers INNER JOIN
Products ON Suppliers.SupplierID = Products.SupplierID CROSS JOIN
Customers INNER JOIN
Orders ON Customers.CustomerID = Orders.CustomerID
WHERE Orders.OrderDate <='1/1/1999'
ORDER BY Suppliers.SupplierID
答案 1 :(得分:1)
您可以选择所有列来区分:
SELECT DISTINCT
Suppliers.SupplierID, Customers.CompanyName, Products.ProductName,
Orders.OrderDate
FROM
Suppliers INNER JOIN
Products ON Suppliers.SupplierID = Products.SupplierID CROSS JOIN
Customers INNER JOIN
Orders ON Customers.CustomerID = Orders.CustomerID
WHERE
Orders.OrderDate <='1/1/1999'
ORDER BY
Suppliers.SupplierID
如果您需要仅SupplierID
区分,请使用或使用group。 DISTINCT
不是函数,因此DISTINCT(Suppliers.SupplierID)
与在DISTINCT
之后简单地放置SELECT
单词相同(参见下面的第2条参考文献)。
供参考: