gpt4 book ai didi

MySql求和查询

转载 作者:行者123 更新时间:2023-11-29 14:50:00 27 4
gpt4 key购买 nike

我需要一个 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;

请注意,第一行将是累计总和。

ROLLUP reference

关于MySql求和查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5964705/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com