我有一个包含列 ID(id order)和 IDU(id user)的表“orders”。 我想这样计算:
50 person have 1 order
30 person have 2 order
etc...
我的代码(不好)
SELECT DISTNICT count(id), count(idu) FROM orders GROUP BY idu
请帮助我:)
最佳答案
我将其称为直方图查询的直方图。您想知道有多少用户拥有给定的订单数。解决方案是使用子查询和 group by
两次:
select cnt, count(*), min(idu), max(idu)
from (select idu, count(*) as cnt
from orders
group by idu
) ou
group by cnt;
运行此类查询时,我通常会包含用户 ID 的最小值和最大值,因此我可以轻松找到用户示例(通常是那些有很多订单的用户)。
关于php - MySQL 选择计数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39591762/