MySql行转列&列转行方式

 更新时间:2024年11月05日 09:06:26   作者:江南春失忆梦i  
在MySQL数据库管理中,行转列和列转行是常见的数据处理需求,行转列通常涉及将表中的行数据按照某种规则转换成列形式,常用于报表生成、数据分析等场景,列转行则是将原本以列形式存储的数据转换成行形式,以便于进行进一步的数据处理或分析

MySql行转列&列转行

行转列

创建语句:

create table test1(
id int  auto_increment primary key ,
name varchar(255),
course varchar(255),
score int
)

insert into test1(name,course,score) values ('张三','语文',120);
insert into test1(name,course,score) values ('张三','数学',100);
insert into test1(name,course,score) values ('张三','英语',82);
insert into test1(name,course,score) values ('李四','语文',89);
insert into test1(name,course,score) values ('李四','数学',99);
insert into test1(name,course,score) values ('李四','英语',87);
insert into test1(name,course,score) values ('王五','语文',78);
insert into test1(name,course,score) values ('王五','数学',85);
insert into test1(name,course,score) values ('王五','英语',145);
insert into test1(name,course,score) values ('王五','物理',40);
insert into test1(name,course,score) values ('王五','化学',62);

原始数据:

1、第一种方法:

-- 使用case when then else,这里使用sum函数也可以
select name,
max(case course when '语文' then score else 0 end)as 'chinese',
max(case course when '数学' then score else 0 end)as 'math',
max(case course when '英语' then score else 0 end)as 'english',
max(case course when '物理' then score else 0 end)as 'wuli',
max(case course when '化学' then score else 0 end)as 'huaxue'
from test1 group by name

第一种结果:

2、第二种方法:

-- 使用if语句,这里使用sum函数也可以
select name,
max(if(course = '语文',score,0))as 'chinese',
max(if(course = '数学',score,0))as 'math',
max(if(course = '英语',score,0))as 'english',
max(if(course = '物理',score,0))as 'wuli',
max(if(course = '化学',score,0))as 'huaxue'
from test1 group by name

第二种结果:

3、第三种方法:

-- 动态拼接sql语句,不管多少行都会转列
set @sql = null;
select group_concat(distinct concat('max(if(a.course = ''',a.course,''', a.score, 0)) as ''',a.course, '''')) into @sql from test1 a;
set @sql = concat('select name,', @sql, 'from test1 a group by a.name' );
prepare stmt from @sql;  -- 动态生成脚本,预备一个语句
execute stmt;            -- 动态执行脚本,执行预备的语句
deallocate prepare stmt; -- 释放预备的语句 

-- 通过这个查询拼接的sql
select @sql

第三种结果:

查询出来的sql结果:

-- 查询出来的结果
select name,max(if(a.course = '语文', a.score, 0)) as '语文',max(if(a.course = '数学', a.score, 0)) as '数学',max(if(a.course = '英语', a.score, 0)) as '英语',max(if(a.course = '物理', a.score, 0)) as '物理',max(if(a.course = '化学', a.score, 0)) as '化学',max(if(a.course = '政治', a.score, 0)) as '政治'from test1 a group by a.name

4、第四种方法:

-- 使用distinct
select distinct  a.name,  
(select score from test1 b where a.name=b.name and b.course='语文' ) as 'chinese',  
(select score from test1 b where a.name=b.name and b.course='数学' ) as 'math',
(select score from test1 b where a.name=b.name and b.course='英语' ) as 'english',  
(select score from test1 b where a.name=b.name and b.course='物理' ) as 'wuli',  
(select score from test1 b where a.name=b.name and b.course='化学' ) as 'huaxue'  
from test1 a

第四种结果:

5、第五种方法:

以下三种方法是可以做统计的用的,有三种统计写法,有需求的话可以使用

-- 使用with rollup统计第一种
select ifnull(name,'总计') as name,
max(if(course = '语文',score,0))as 'chinese',
max(if(course = '数学',score,0))as 'math',
max(if(course = '英语',score,0))as 'english',
max(if(course = '物理',score,0))as 'wuli',
max(if(course = '化学',score,0))as 'huaxue',
sum(IF(course='total',score,0)) as 'total'
from (select name,ifnull(course,'total') as course,sum(score) as score
from test1 group by name, course with rollup having name is not null )as a group by name with rollup;


-- 使用with rollup与union all统计第二种
select name,
max(if(course = '语文',score,0))as 'chinese',
max(if(course = '数学',score,0))as 'math',
max(if(course = '英语',score,0))as 'english',
max(if(course = '物理',score,0))as 'wuli',
max(if(course = '化学',score,0))as 'huaxue',
sum(score) as total from test1 group by name
union all
select 'total',
max(if(course = '语文',score,0))as 'chinese',
max(if(course = '数学',score,0))as 'math',
max(if(course = '英语',score,0))as 'english',
max(if(course = '物理',score,0))as 'wuli',
max(if(course = '化学',score,0))as 'huaxue',
sum(score) from test1


-- 使用if与with rollup统计
select ifnull(name,'total') as name,
max(if(course = '语文',score,0))as 'chinese',
max(if(course = '数学',score,0))as 'math',
max(if(course = '英语',score,0))as 'english',
max(if(course = '物理',score,0))as 'wuli',
max(if(course = '化学',score,0))as 'huaxue',
sum(score) AS total 
from test1
group by name with rollup

第五种结果:

以上三种语句结果都是一样的

6、第六种方法:

-- 使用group_concat,这个一般不推荐
select name,
group_concat(course,':',score separator '@')as course
from test1 group by name

第六种结果:

7、第七中方法:

-- 这个也不推荐使用
set @EE='';
select @EE :=concat(@EE,'sum(if(course= \'',course,'\',score,0)) as ',course, ',') as aa from (select distinct course from test1) a ;
set @QQ = concat('select ifnull(name,\'total\')as name,',@EE,' sum(score) as total from test1 group by name with rollup');
-- SELECT @QQ;
prepare stmt from @QQ;
execute stmt;
deallocate prepare stmt;

第七种结果:

列转行

创建语句:

create table test2(
id int  auto_increment primary key ,
name varchar(255) ,
chinese int,
math int,
english int,
wuli int,
huaxue int
)

insert into test2(name,chinese,math,english,wuli,huaxue) values ('张三',110,120,85,null,null);
insert into test2(name,chinese,math,english,wuli,huaxue) values ('李四',130,88,89,null,null);
insert into test2(name,chinese,math,english,wuli,huaxue) values ('王五',93,124,87,98,67);

原始数据:

1、第一种方法:

有两种写法:

union与union all的区别就是:
union可以去除重复结果集,union all不会去除重复的结果集  
-- 第一种使用union实现列传行
select name,'语文' as course, chinese as 'score' from test2 union 
select name,'数学' as course, math as 'score' from test2 union 
select name,'英语' as course, english as 'score' from test2 union 
select name,'物理' as course, wuli as 'score' from test2 union 
select name,'化学' as course, huaxue as 'score' from test2 order by name asc

-- 第二种写法可以对null值结果集处理
select * from
(select name,'语文' as course, chinese as 'score' from test2 union 
select name,'数学' as course, math as 'score' from test2 union 
select name,'英语' as course, english as 'score' from test2 union 
select name,'物理' as course, wuli as 'score' from test2 union 
select name,'化学' as course, huaxue as 'score' from test2)a
where a.score is not null order by name asc

第一种结果:

有null结果:

没有null结果:

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • MySQL使用 B+ 树作为索引结构的示例详解

    MySQL使用 B+ 树作为索引结构的示例详解

    在日常开发中,SQL 查询速度往往决定了系统响应的快慢,而索引的底层结构直接影响数据库性能,本文我们就来揭开 B+ 树在 MySQL 中大行其道的秘密吧
    2025-07-07
  • 你真的会用Mysql的explain吗

    你真的会用Mysql的explain吗

    explain显示了mysql如何使用索引来处理select语句以及连接表,可以帮助选择更好的索引和写出更优化的查询语句,下面这篇文章主要给大家介绍了关于Mysql中explain用法的相关资料,需要的朋友可以参考下
    2022-03-03
  • mysql 5.7.11 zip安装配置方法图文教程

    mysql 5.7.11 zip安装配置方法图文教程

    这篇文章主要为大家详细介绍了mysql 5.7.11 安装配置方法图文教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-03-03
  • mysql数据库优化需要遵守的原则

    mysql数据库优化需要遵守的原则

    这是我在网上看到的一篇不错的mysql数据库优化文章,拿出来和大家分享,希望可以帮助你们
    2012-12-12
  • mysql查看回滚日志的方法步骤

    mysql查看回滚日志的方法步骤

    在MySQL数据库中,操作回滚日志是一种记录数据库中事务操作的重要机制,本文主要介绍了mysql查看回滚日志的方法步骤,具有一定的参考价值,感兴趣的可以了解一下
    2024-05-05
  • 解决mysql登录错误:''Access denied for user ''root''@''localhost''

    解决mysql登录错误:''Access denied for user ''root''@''localhost''

    这篇文章主要介绍了mysql登录错误:'Access denied for user 'root'@'localhost',本文给出了操作过程及注意事项,需要的朋友可以参考下
    2019-11-11
  • MySQL主从状态检查的实现

    MySQL主从状态检查的实现

    这篇文章主要介绍了MySQL主从状态检查的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-02-02
  • C3P0连接池+MySQL的配置及wait_timeout问题的解决方法

    C3P0连接池+MySQL的配置及wait_timeout问题的解决方法

    这篇文章主要介绍了C3P0连接池+MySQL的配置及wait_timeout问题的解决方法,本文介绍的非常详细,具有参考借鉴价值,需要的朋友可以参考下
    2016-09-09
  • MySQL内存使用之线程独享介绍

    MySQL内存使用之线程独享介绍

    这篇文章主要介绍了MySQL内存使用之线程独享介绍,线程独享内存主要用于各客户端连接线程存储各种操作的独享数据,如线程栈信息,分组排序操作,数据读写缓冲,结果集暂存等等,而且大多数可以通过相关参数来控制内存的使用量,需要的朋友可以参考下
    2014-12-12
  • Mysql数据库编码问题 (修改数据库,表,字段编码为utf8)

    Mysql数据库编码问题 (修改数据库,表,字段编码为utf8)

    个人建议,数据库字符集尽量使用 utf8(HTML页面对应的是utf-8),以使你的数据能很顺利的实现迁移
    2011-10-10

最新评论