订购有序的SQL查询

时间:2014-09-03 02:22:40

标签: mysql sql

所以我有这个查询来获得大多数订单的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;

1 个答案:

答案 0 :(得分:1)

使用子查询:

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;