create table t_earn_money( id bigint(20) AUTO_INCREMENT, nick_name varchar(20) , money int(8) , item_name varchar(20), primary key (id) ); select * from t_earn_money; insert into t_earn_money (nick_name,money,item_name) values ('zs',10,'上班'); insert into t_earn_money (nick_name,money,item_name) values ('zs',20,'直播'); insert into t_earn_money (nick_name,money,item_name) values ('zs',30,'外卖'); insert into t_earn_money (nick_name,money,item_name) values ('wt',5,'上班'); insert into t_earn_money (nick_name,money,item_name) values ('wt',99,'直播'); select nick_name, sum(case item_name when '上班' then money else 0 end) as 上班, sum(case item_name when '直播' then money else 0 end )as 直播, sum(case item_name when '外卖' then money else 0 end )as 外卖 from t_earn_money group by nick_name; select nick_name, sum(case when item_name ='上班' or item_name= '外卖' then money else 0 end) as 上班, sum(case item_name when '直播' then money else 0 end )as 直播, sum(case item_name when '外卖' then money else 0 end )as 外卖 from t_earn_money group by nick_name;