按条目数排序MYSQL数据

时间:2010-05-22 20:33:10

标签: sql mysql

我想知道如何根据条目数对mysql数据进行排序。

我正在这样做,所以我可以有一个顶级购买的页面,所以它必须从表中检索所有product_id,然后按照显示的最多时间对它们进行排序,将其限制为10或者其他

谢谢!

1 个答案:

答案 0 :(得分:2)

使用:

  SELECT p.product_id,
         COUNT(*) AS num_orders
    FROM PRODUCTS p
GROUP BY p.product_id
ORDER BY num_orders DESC --to put highest sales at the top of the list
   LIMIT 10 -- Query will return 10 records, max
相关问题