我有这样的订单表
Id | Order_id | item_id | restaurant_id | price
--------------------------------------------------
1 | 1 | 1 | 2 | 200
---------------------------------------------------
2 | 2 | 1 | 2 | 200
---------------------------------------------------
3 | 3 | 2 | 2 | 400
如何在restaurant_id为2的情况下按升序从订单表中获取热门商品。 我想要这样的东西 热门商品
1
2
我尝试但是我只得到总计数而且我被困在那里
SELECT COUNT(item_id) FROM `item_orders` WHERE restaurant_id = 1
答案 0 :(得分:2)
通过这种方式,您可以获得每个项目的计数并按计数
进行排序SELECT item_id, COUNT(*)
FROM `item_orders`
WHERE restaurant_id = 1
GROUP BY item_id
ORDER BY COUNT(*) DESC
答案 1 :(得分:1)
试试这个;
select * from
(SELECT COUNT(item_id) as count FROM `item_orders`
WHERE restaurant_id = 1) as T
order by `count` asc