目录
一、行转列
1、使用case…when…then
2、使用sum(if()) 生成列
3、使用sum(if()) 生成列 with rollup 生成汇总行
4、使用sum(if()) 生成列,直接生成汇总结果,不再利用子查询
5、使用sum(if()) 生成列 union 生成汇总行,并利用 ifnull将汇总行标题显示为 total
6、动态查询列值不确定的情况
7、合并字段显示:group_concat()
二、列转行
————————————————
即将原本同一列下多行的不同内容作为多个字段,输出对应内容。
建表语句
drop table if exists tb_score;
create table tb_score(
id int(11) not null auto_increment,
userid varchar(20) not null comment '用户id',
subject varchar(20) comment '科目',
score double comment '成绩',
primary key(id)
)engine = innodb default charset = utf8;
插入数据
insert into tb_score(userid,subject,score) values ('001','语文',90);
insert into tb_score(userid,subject,score) values ('001','数学',92);
insert into tb_score(userid,subject,score) values ('001','英语',80);
insert into tb_score(userid,subject,score) values ('002','语文',88);
insert into tb_score(userid,subject,score) values ('002','数学',90);
insert into tb_score(userid,subject,score) values ('002','英语',75.5);
insert into tb_score(userid,subject,score) values ('003','语文',70);
insert into tb_score(userid,subject,score) values ('003','数学',85);
insert into tb_score(userid,subject,score) values ('003','英语',90);
insert into tb_score(userid,subject,score) values ('003','政治',82);
查询数据表中的内容(即转换前的结果)
select * from tb_score
先来看一下转换后的结果:
可以看出,这里行转列是将原来的subject字段的多行内容选出来,作为结果集中的不同列,并根据userid进行分组显示对应的score。
1、使用case…when…then 进行行转列
select userid,
sum(case `subject` when '语文' then score else 0 end) as '语文',
sum(case `subject` when '数学' then score else 0 end) as '数学',
sum(case `subject` when '英语' then score else 0 end) as '英语',
sum(case `subject` when '政治' then score else 0 end) as '政治'
from tb_score
group by userid
2、使用if() 进行行转列:
select userid,
sum(if(`subject`='语文',score,0)) as '语文',
sum(if(`subject`='数学',score,0)) as '数学',
sum(if(`subject`='英语',score,0)) as '英语',
sum(if(`subject`='政治',score,0)) as '政治'
from tb_score
group by userid
注意点:
(1)sum() 是为了能够使用group by根据userid进行分组,因为每一个userid对应的subject="语文"的记录只有一条,所以sum() 的值就等于对应那一条记录的score的值。
假如userid =‘001’ and subject=‘语文’ 的记录有两条,则此时sum() 的值将会是这两条记录的和,同理,使用max()的值将会是这两条记录里面值最大的一个。但是正常情况下,一个user对应一个subject只有一个分数,因此可以使用sum()、max()、min()、avg()等聚合函数都可以达到行转列的效果。
(2)if(subject
=‘语文’,score,0) 作为条件,即对所有subject='语文’的记录的score字段进行sum()、max()、min()、avg()操作,如果score没有值则默认为0。
3、利用sum(if()) 生成列 with rollup 生成汇总行,并利用 ifnull将汇总行标题显示为total
select ifnull(userid,'total') as userid,
sum(if(`subject`='语文',score,0)) as 语文,
sum(if(`subject`='数学',score,0)) as 数学,
sum(if(`subject`='英语',score,0)) as 英语,
sum(if(`subject`='政治',score,0)) as 政治,
sum(if(`subject`='total',score,0)) as total
from(
select userid,ifnull(`subject`,'total') as `subject`,sum(score) as score
from tb_score
group by userid,`subject`
with rollup
having userid is not null
)as a
group by userid
with rollup;
运行结果:
4、利用sum(if()) 生成列 union 生成汇总行,并利用 ifnull将汇总行标题显示为 total
select userid,
sum(if(`subject`='语文',score,0)) as 语文,
sum(if(`subject`='数学',score,0)) as 数学,
sum(if(`subject`='英语',score,0)) as 英语,
sum(if(`subject`='政治',score,0)) as 政治,
sum(score) as total
from tb_score
group by userid
union
select 'total',sum(if(`subject`='语文',score,0)) as 语文,
sum(if(`subject`='数学',score,0)) as 数学,
sum(if(`subject`='英语',score,0)) as 英语,
sum(if(`subject`='政治',score,0)) as 政治,
sum(score) from tb_score
运行结果:
5、利用sum(if()) 生成列,直接生成结果不再利用子查询
select ifnull(userid,'total') as userid,
sum(if(`subject`='语文',score,0)) as 语文,
sum(if(`subject`='数学',score,0)) as 数学,
sum(if(`subject`='英语',score,0)) as 英语,
sum(if(`subject`='政治',score,0)) as 政治,
sum(score) as total
from tb_score
group by userid with rollup;
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
运行结果:
6、动态,适用于列不确定情况
set @ee='';
select @ee :=concat(@ee,'sum(if(subject= \'',subject,'\',score,0)) as ',subject, ',') as aa from (select distinct subject from tb_score) a ;
set @qq = concat('select ifnull(userid,\'total\')as userid,',@ee,' sum(score) as total from tb_score group by userid with rollup');
-- select @qq;
prepare stmt from @qq;
execute stmt;
deallocate prepare stmt;
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
运行结果:
7、合并字段显示:利用group_concat()
select userid,group_concat(`subject`,":",score)as 成绩 from tb_score
group by userid
- 1
- 2
运行结果:
group_concat(),手册上说明:该函数返回带有来自一个组的连接的非null值的字符串结果。
比较抽象,难以理解。通俗点理解,其实是这样的:group_concat()会计算哪些行属于同一组,将属于同一组的列显示出来。要返回哪些列,由函数参数(就是字段名)决定。分组必须有个标准,就是根据group by指定的列进行分组。
结论:group_concat()函数可以很好的建属于同一分组的多个行转化为一个列。
建表语句:
create table tb_score1(
id int(11) not null auto_increment,
userid varchar(20) not null comment '用户id',
cn_score double comment '语文成绩',
math_score double comment '数学成绩',
en_score double comment '英语成绩',
po_score double comment '政治成绩',
primary key(id)
)engine = innodb default charset = utf8;
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
插入数据:
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
- 2
- 3
查询数据表中的内容(即转换前的结果)
select * from tb_score1
转换后:
本质是将userid的每个科目分数分散成一条记录显示出来。
直接上sql:
select userid,'语文' as course,cn_score as score from tb_score1
union all
select userid,'数学' as course,math_score as score from tb_score1
union all
select userid,'英语' as course,en_score as score from tb_score1
union all
select userid,'政治' as course,po_score as score from tb_score1
order by userid
这里将每个userid对应的多个科目的成绩查出来,通过union all将结果集加起来,达到上图的效果。
附:union与union all的区别(摘)
-
对重复结果的处理:union会去掉重复记录,union all不会;
-
对排序的处理:union会排序,union all只是简单地将两个结果集合并;
-
效率方面的区别:因为union 会做去重和排序处理,因此效率比union all慢很多;