Mysql查询结果分组

数据表

img

查询结果需要这样输出:
{
typy:今日上报
num:45
},
{
typy:办理中
num:14
},
{
typy:待受理
num:15
},
{
typy:已办理
num:10
}
SQL语句如何写,请指教

你是只需要一个汇总sql,还是要用sql输出json数组?
还有你mysql的版本是多少?
假设只是汇总数据

select 
sum(今日上报) 今日上报,
sum(办理中) 办理中,
sum(待受理) 待受理,
sum(已办理) 已办理
from 表

假设要进行行列转换

select '今日上报' type,sum(今日上报) num fromunion all
select '办理中' type,sum(办理中) fromunion all
select '待受理' type,sum(待受理) fromunion all
select '已办理' type,sum(已办理) from

假设要转换成json,并且是mysql8.0以上

with t as (
select '今日上报' type,sum(今日上报) num fromunion all
select '办理中' type,sum(办理中) fromunion all
select '待受理' type,sum(待受理) fromunion all
select '已办理' type,sum(已办理) from 表)
select JSON_ARRAYAGG(JSON_OBJECT('type',type,'num',num)) from t