我需要一个 MySql 查询来执行以下操作: 选择表中的所有项目,按类型对它们进行分组,按计数类型对它们进行排序,并对所有计数求和。
这是我到目前为止所做的:
select type, count(*) as cnt from $tbl_name group by type order by count(*) desc;
这仅给我每个组的计数。我应该添加什么,以便此代码还显示总计数(每个组的计数总和)。
最佳答案
select * from (
select type as type,
count(*) as cnt
from $tbl_name
group by type WITH ROLLUP) as inner_table
order by cnt desc;
请注意,第一行将是累计总和。
关于MySql求和查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5964705/