mysql 金币兑换记录表中,一个字段对应多种数值,区分求和,并按id分组返回

图片说明

金币记录表格里,既记录领取的金币也记录消耗的金币
怎么按照ID分别输出正数和负数的和?
比如
【ID 获取金币 消耗金币】
这样

select id, sum('积分') as score from table group by id;

SELECT ids as ID,sum(case when fenshu>0 then fenshu ELSE 0 end) as '获取金币',sum(case when fenshu<0 then fenshu ELSE 0 end) as '消耗金币' from book GROUP BY ids

亲测可用,有问题直接加QQ在线解答