我想要的是"获得前5个不同客户的订单ID值"。当我将下面的查询限制为5时,它只会按预期返回前4个客户的订单ID值。(在Horn附近有两个不同的订单ID)是否有办法获得前5个不同客户的订单ID值?
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
LEFT JOIN Orders
ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;
CustomerName OrderID
Alfreds Futterkiste 10001
Ana Trujillo Emparedados y helados 10308
Antonio Moreno Taquería 10365
Around the Horn 10355
Around the Horn 10383
B's Beverages 10289
Antonio baa 10444
.... ....
编辑:我想要的是前5个不同的客户;
Alfreds Futterkiste 10001 (first customer)
Ana Trujillo Emparedados y helados 10308 (second customer)
Antonio Moreno Taquería 10365 (third customer)
Around the Horn 10355 (forth customer)
Around the Horn 10383 (forth customer)
B's Beverages 10289 (fifth customer)
这里are我正在处理的表格。
答案 0 :(得分:1)
试试这个。
SELECT DISTINCT Orders.OrderID, CC.CustomerName
FROM Customers CC
LEFT JOIN Orders
ON CC.CustomerID=Orders.CustomerID
inner join
(
SELECT DISTINCT Customers.CustomerName
FROM Customers
LEFT JOIN Orders
ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName
LIMIT 5
) C
ON C.CustomerName=CC.CustomerName
ORDER BY CC.CustomerName
答案 1 :(得分:-1)
使用“SELECT TOP 5”限制5中的查询并添加额外的WHERE子句,“WHERE Customers.OrderdId IS NOT NULL”