【学亮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

 

posted @ 2019-02-01 20:51  学亮编程手记  阅读(211)  评论(0编辑  收藏  举报