MySQL行列互转
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL行列互转相关的知识,希望对你有一定的参考价值。
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行转列
将原本同一列下多行的不同内容作为多个字段,输出对应内容。
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.列转行
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;
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 ;
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行列互转的主要内容,如果未能解决你的问题,请参考以下文章