【学亮IT手记】MySql行列转换案例

create table score(
    name varchar(10),
    math int,
    english int
);
insert into score VALUES('',100,92);
insert into score VALUES('',85,98);
insert into score VALUES('',90,95);
insert into score VALUES('',88,96);
SHOW tables;
SELECT * from score;
--行列转换:
select c2 as '课程', SUM(if(c1='',c3,0)) '', SUM(if(c1='',c3,0)) '', SUM(if(c1='',c3,0)) '', SUM(if(c1='',c3,0)) '' from( select name c1,'math' c2,math c3 from score GROUP BY name union select name,'english' as c2,english from score GROUP BY name )tt group by c2
原文地址:https://www.cnblogs.com/niwotaxuexiba/p/10346981.html