日期:2014-05-16 浏览次数:20811 次
create table lk4( userid mediumint(3) unsigned zerofill not null, mhtype int not null, mh int not null); insert into lk4 values (001,1,5), (001,1,3), (001,2,4), (001,3,9), (002,5,5), (002,6,7), (002,3,4), (002,3,9); select userid, sum((case when mhtype=1 then mh else 0 end )) as 'mhtype1', sum((case when mhtype=2 then mh else 0 end )) as 'mhtype2', sum((case when mhtype=3 then mh else 0 end )) as 'mhtype3', sum((case when mhtype=4 then mh else 0 end )) as 'mhtype4', sum((case when mhtype=5 then mh else 0 end )) as 'mhtype5', sum((case when mhtype=6 then mh else 0 end )) as 'mhtype6', sum(mh) as 'allmh' from lk4 group by userid;