gpt4 book ai didi

mysql - 订购一个有序的sql查询

转载 作者:行者123 更新时间:2023-11-29 00:09:58 25 4
gpt4 key购买 nike

所以我有这个查询来获取订单最多的 8 艘船,在这个查询之后我想让他们按 ID 排序

SELECT Shippers.ShipperName,COUNT(Orders.OrderID) AS NumberOfOrders 
FROM Shippers
LEFT JOIN Orders
ON Orders.ShipperID=Shippers.ShipperID
GROUP BY ShipperName
ORDER BY NumberofOrders DESC
LIMIT 8;

最佳答案

使用子查询:

SELECT ShipperName, NumberOfOrders
FROM (SELECT s.ShipperName, s.ShipperId, COUNT(o.OrderID) AS NumberOfOrders
FROM Shippers s LEFT JOIN
Orders o
ON o.ShipperID = s.ShipperID
GROUP BY ShipperName, s.ShipperId
ORDER BY NumberofOrders DESC
LIMIT 8
) s
ORDER BY ShipperId;

关于mysql - 订购一个有序的sql查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25635254/

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