所以我有这个查询来获取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/

10-11 01:55