参考博主网址mysql根据某个字段切割并分组,再统计
数据库表字段结构类似如下所示:
注:此sql不和上面的表结构相对应
select pcu.user_name userName, IFNULL(sum(investment_rmb_fee), 0) investmentRmbFee from (SELECT o.investment_rmb_fee, SUBSTRING_INDEX( SUBSTRING_INDEX( pcp.principal_by, ',', b.help_topic_id + 1 ), ',',- 1 ) AS id
FROM `a` AS pcp JOIN help_topic AS b ON b.help_topic_id < ( length( pcp.principal_by ) - length( REPLACE ( pcp.principal_by, ',', '' ) ) + 1 ) left join a_other o on o.project_id = pcp.project_id where pcp.principal_by is not null and pcp.principal_by != "" ) splitPcp left join u pcu on pcu.company_user_id = splitPcp.id where pcu.user_name is not null
group by id,pcu.user_name ORDER BY investmentRmbFee desc
分组统计每个的金额总数查询结果: