首页 > 数据库 >MySQL行列互转

MySQL行列互转

时间:2023-03-05 11:31:40浏览次数:47  
标签:chinese sum userid 行列 score MySQL 互转 tb subject

sudo service mysql start --启动mysql服务
mysql -u root --使用root用户登录
quit /exit --退出
set names utf8; --设置传输编码,避免中文乱码
source d:/xxx.sql; --source 文件路径
create table tb_score(
id int(11) not null auto_increment,
userid varchar(20) not null,
subject varchar(20),
score double,
primary key(id))
engine=innodb default charset=utf8;
tab1:
INSERT INTO tb_score(userid,subject,score) VALUES ('001','chinese',90);
INSERT INTO tb_score(userid,subject,score) VALUES ('001','math',92);
INSERT INTO tb_score(userid,subject,score) VALUES ('001','english',80);
INSERT INTO tb_score(userid,subject,score) VALUES ('002','chinese',88);
INSERT INTO tb_score(userid,subject,score) VALUES ('002','math',90);
INSERT INTO tb_score(userid,subject,score) VALUES ('002','english',75.5);
INSERT INTO tb_score(userid,subject,score) VALUES ('003','chinese',70);
INSERT INTO tb_score(userid,subject,score) VALUES ('003','math',85);
INSERT INTO tb_score(userid,subject,score) VALUES ('003','english',90);
INSERT INTO tb_score(userid,subject,score) VALUES ('003','cs',82);
tab2:
INSERT INTO tb_score1(userid,cn_score,math_score,en_score,po_score) VALUES ('001',90,92,80,0);
INSERT INTO tb_score1(userid,cn_score,math_score,en_score,po_score) VALUES ('002',88,90,75.5,0);
INSERT INTO tb_score1(userid,cn_score,math_score,en_score,po_score) VALUES ('003',70,85,90,82);

1.mysql行转列
将原本同一列下多行的不同内容作为多个字段,输出对应内容。


MySQL行列互转_行转列

原表.png


MySQL行列互转_行转列_02

转换后.png


a.
select userid ,
sum(case subject when 'chinese' then score else 0 end) as chinese,
sum(case subject when 'math' then score else 0 end ) as math ,
sum(case subject when 'english' then score else 0 end) as english ,
sum(case subject when 'cs' then score else 0 end) as cs
from tb_score group by userid;
b.
select userid,
sum(if(subject='chinese',score,0))as chinese,
sum(if(subject='math',score,0))as math,
sum(if(subject='english',score,0))as english,
sum(if(subject='cs',score,0)) as cs
from tb_score group by userid;

注意点:

(1)SUM() 是为了能够使用GROUP BY根据userid进行分组,因为每一个userid对应的subject="chinese"的记录只有一条,所以SUM() 的值就等于对应那一条记录的score的值。
假如userid ='001' and subject='chinese' 的记录有两条,则此时SUM() 的值将会是这两条记录的和,同理,使用Max()的值将会是这两条记录里面值最大的一个。但是正常情况下,一个user对应一个subject只有一个分数,因此可以使用SUM()、MAX()、MIN()、AVG()等聚合函数都可以达到行转列的效果。
(2)IF(​​subject​​='chinese',score,0) 作为条件,即对所有subject='chinese'的记录的score字段进行SUM()、MAX()、MIN()、AVG()操作,如果score没有值则默认为0。
2.列转行

MySQL行列互转_行转列_03

原表.png

MySQL行列互转_mysql_04

转换后.png


select userid,'chinese' as course,cn_score as score from tb_score2 
union all
select userid,'math' as course,math_score as score from tb_score2
union all
select userid,'english' as course,en_score as score from tb_score2
union all
select userid,'cs' as course ,cs_score as score from tb_score2
order by userid;

注意点:UNION与UNION ALL的区别

1.对重复结果的处理:UNION会去掉重复记录,UNION ALL不会;
2.对排序的处理:UNION会排序,UNION ALL只是简单地将两个结果集合并;
3.效率方面的区别:因为UNION 会做去重和排序处理,因此效率比UNION ALL慢很多;

3.利用SUM(IF()) 生成列 + UNION 生成汇总行,并利用 IFNULL将汇总行标题显示为 Total


select userid, 
sum(if(subject='chinese',score,0))as chinese,
sum(if(subject='math',score,0)) as math,
sum(if(subject='english',score,0))as english,
sum(if(subject='cs',score,0))as cs,
sum(score) as total
from tb_score group by userid;

MySQL行列互转_mysql_05

行求和


select 'total', 
sum(if(subject='chinese',score,0))as chinese,
sum(if(subject='math',score,0)) as math,
sum(if(subject='english',score,0))as english,
sum(if(subject='cs',score,0))as cs,
sum(score)
from tb_score ;

MySQL行列互转_mysql_06

列求和

4.利用SUM(IF()) 生成列,with rollup直接生成结果


select  ifnull (userid,'total'), 
sum(if(subject='chinese',score,0))as chinese,
sum(if(subject='math',score,0)) as math,
sum(if(subject='english',score,0))as english,
sum(if(subject='cs',score,0))as cs,
sum(score)
from tb_score
group by userid with rollup;

MySQL行列互转_行转列_07



标签:chinese,sum,userid,行列,score,MySQL,互转,tb,subject
From: https://blog.51cto.com/u_13146445/6101343

相关文章