http://blog.csdn.net/sinat_27406925/article/details/77507478

mysql 行列转换 ,在项目中应用的极其频繁,尤其是一些金融项目里的报表。其中最为头痛的就是多行转多列,动态的列行转换。最近在研究这些行里转换,还是从最为简单的行列转换开始。

sql 脚本

-- 创建表  学生表
CREATE TABLE `student` (
`stuid` VARCHAR() NOT NULL COMMENT '学号',
`stunm` VARCHAR() NOT NULL COMMENT '学生姓名',
PRIMARY KEY (`stuid`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB; -- 课程表 CREATE TABLE `courses` (
`courseno` VARCHAR() NOT NULL,
`coursenm` VARCHAR() NOT NULL,
PRIMARY KEY (`courseno`)
)
COMMENT='课程表'
COLLATE='utf8_general_ci'
ENGINE=InnoDB; -- 成绩表
CREATE TABLE `score` (
`stuid` VARCHAR() NOT NULL,
`courseno` VARCHAR() NOT NULL,
`scores` FLOAT NULL DEFAULT NULL,
PRIMARY KEY (`stuid`, `courseno`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB; -- 插入数据 -- 学生表数据 Insert Into student (stuid, stunm) Values('', '张三');
Insert Into student (stuid, stunm) Values('', '李四');
Insert Into student (stuid, stunm) Values('', '赵二');
Insert Into student (stuid, stunm) Values('', '王五');
Insert Into student (stuid, stunm) Values('', '刘青');
Insert Into student (stuid, stunm) Values('', '周明'); -- 课程表数据
Insert Into courses (courseno, coursenm) Values('C001', '大学语文');
Insert Into courses (courseno, coursenm) Values('C002', '新视野英语');
Insert Into courses (courseno, coursenm) Values('C003', '离散数学');
Insert Into courses (courseno, coursenm) Values('C004', '概率论与数理统计');
Insert Into courses (courseno, coursenm) Values('C005', '线性代数');
Insert Into courses (courseno, coursenm) Values('C006', '高等数学(一)');
Insert Into courses (courseno, coursenm) Values('C007', '高等数学(二)'); -- 成绩表数据 Insert Into score(stuid, courseno, scores) Values('', 'C001', );
Insert Into score(stuid, courseno, scores) Values('', 'C001', );
Insert Into score(stuid, courseno, scores) Values('', 'C001', );
Insert Into score(stuid, courseno, scores) Values('', 'C001', );
Insert Into score(stuid, courseno, scores) Values('', 'C001', );
Insert Into score(stuid, courseno, scores) Values('', 'C001', );
Insert Into score(stuid, courseno, scores) Values('', 'C002', );
Insert Into score(stuid, courseno, scores) Values('', 'C002', );
Insert Into score(stuid, courseno, scores) Values('', 'C002', );
Insert Into score(stuid, courseno, scores) Values('', 'C002', );
Insert Into score(stuid, courseno, scores) Values('', 'C002', );
Insert Into score(stuid, courseno, scores) Values('', 'C002', );
Insert Into score(stuid, courseno, scores) Values('', 'C003', );
Insert Into score(stuid, courseno, scores) Values('', 'C003', );
Insert Into score(stuid, courseno, scores) Values('', 'C003', );
Insert Into score(stuid, courseno, scores) Values('', 'C003', );
Insert Into score(stuid, courseno, scores) Values('', 'C003', );
Insert Into score(stuid, courseno, scores) Values('', 'C003', );
Insert Into score(stuid, courseno, scores) Values('', 'C004', );
Insert Into score(stuid, courseno, scores) Values('', 'C004', );
Insert Into score(stuid, courseno, scores) Values('', 'C004', );
Insert Into score(stuid, courseno, scores) Values('', 'C004', );
Insert Into score(stuid, courseno, scores) Values('', 'C004', );
Insert Into score(stuid, courseno, scores) Values('', 'C004', );
Insert Into score(stuid, courseno, scores) Values('', 'C005', );
Insert Into score(stuid, courseno, scores) Values('', 'C005', );
Insert Into score(stuid, courseno, scores) Values('', 'C005', ); --
select st.stuid,st.stunm from student st select sc.stuid , sc.courseno,sc.scores from score sc select cs.courseno,cs.coursenm from courses cs

要求: 查询每个学生的 每门课程与每门成绩

 select   st.stuid ID ,  st.stunm 姓名, cs.coursenm 课程名 ,sc.scores 成绩     from  student st, score sc ,courses cs

where st.stuid = sc.stuid and sc.courseno = cs.courseno  

结果: 

这是4列27行

我们行转成列,ID对应姓名对应每门课程对应每门成绩

静态行专列

 select st.stuid 编号, st.stunm 姓名 ,
Max(case c.coursenm when '大学语文' then s.scores else end ) '大学语文',
max(case c.coursenm when '新视野英语' then IFNULL(s.scores,)else end) '新视野英语',
Max(case c.coursenm when '离散数学' then IFNULL(s.scores,) ELSE END) '离散数学',
MAX(case c.coursenm when '概率论与数理统计' then IFNULL(s.scores,) else end) '概率论与数理统计',
MAX(case c.coursenm when '线性代数' then IFNULL(s.scores,) else END) '线性代数',
MAX(case c.coursenm when '高等数学(一)' THEN IFNULL(s.scores,) else end) '高等数学(一)',
MAX(case c.coursenm when '高等数学(二)' THEN IFNULL(s.scores,) else end) '高等数学(二)'
from student st
LEFT JOIN score s on st.stuid = s.stuid
LEFT JOIN courses c on c.courseno = s.courseno
GROUP BY st.stuid

再来看看 运行结果:

这就是实现了简单的行列

再来看看group_concat() 这个函数

group_concat(),手册上说明:该函数返回带有来自一个组的连接的非NULL值的字符串结果。 
比较抽象,难以理解。

通俗点理解,其实是这样的:group_concat()会计算哪些行属于同一组,将属于同一组的列显示出来。要返回哪些列,由函

数参数(就是字段名)决定。分组必须有个标准,就是根据group by指定的列进行分组。

这些都是从网上看到的解释,但还是不好理解,我们直接上代码,看看run出来的结果,根据run之后的结果再回过来看!

select   s.stuid 编号 , GROUP_CONCAT(courseno) 课程号 , GROUP_CONCAT(s.scores)  成绩  from score s GROUP BY  s.stuid

看看运行后的结果: 
之前效果:

非常明显GROUP_CONCAT() 作用 ,将课程号courseno, 成绩 scores 的结果集放在一起。

mysql行转列转换的更多相关文章

  1. MySQL 行转列 -》动态行转列 -》动态行转列带计算

    Pivot Table Using MySQL - A Complete Guide | WebDevZoomhttp://webdevzoom.com/pivot-table-using-mysql ...

  2. MYSQL 行转列 以及基本的聚合函数count,与group by 以及distinct组合使用

    在统计查询中,经常会用到count函数,这里是基础的 MYSQL 行转列 以及基本的聚合函数count,与group by 以及distinct组合使用 -- 创建表 CREATE TABLE `tb ...

  3. mysql行转列 问题 SUM(IF(条件,列值,0))

    sum(if(条件,列值,0))语法用例: select name,sum(if(subject="语文",score,0)) as "语文" from gra ...

  4. mysql行转列、列转行示例

    最近在开发过程中遇到问题,需要将数据库中一张表信息进行行转列操作,再将每列(即每个字段)作为与其他表进行联表查询的字段进行显示. 借此机会,在网上查阅了相关方法,现总结出一种比较简单易懂的方法备用. ...

  5. MySql 行转列 存储过程实现

    同学们在使用mysql的过程中,会遇到一个行转列的问题,就是把多条数据转化成一条数据 用多列显示. 方法1. 实现方式用下面的存储过程,表名对应的修改就行. BEGIN declare current ...

  6. mysql 行转列 列转行

    一.行转列 即将原本同一列下多行的不同内容作为多个字段,输出对应内容. 建表语句 DROP TABLE IF EXISTS tb_score; CREATE TABLE tb_score( id ) ...

  7. MySQL行转列与列转行

    行转列 例如:把图1转换成图2结果展示 图1 图2 CREATE TABLE `TEST_TB_GRADE` ( `ID` ) NOT NULL AUTO_INCREMENT, `) DEFAULT ...

  8. [转]mysql 行转列 列转行

    原文地址:http://www.cnblogs.com/xiaoxi/p/7151433.html 一.行转列 即将原本同一列下多行的不同内容作为多个字段,输出对应内容. 建表语句 DROP TABL ...

  9. mysql行转列,单列转多行

    行转列 使用CASE语句: SELECT SUM(CASE USER_NAME='A' THEN KILLS END) AS 'A', SUM(CASE USERNAME='B' THEN KILL ...

随机推荐

  1. mysql主从复制配置问题

    一,基本步骤 1,创建在主从数据上都创建复制账号,权限选上super, replication slave , replication master(选上这个可以方便从库变成主库): 2,配置主库和备 ...

  2. Extending a logical volume in a virtual machine running Red Hat or Cent OS (1006371)

    Purpose This article provides steps for extending the root partition residing in a logical volume cr ...

  3. SQLAlchemy(2) -- SQLAlchemy的安装

    安装前要先安装好python 1.使用setup.py脚本进行安装C:\> C:\Python27\python.exe .\setup.py installrunning installrun ...

  4. Debian本地镜像长时间不更新

    一.执行apt-get update 使用一个长期未更新的本地源,得到错误的提示: Release file for ... is expired. Updates for this reposito ...

  5. Yii2 使用 faker 生成假数据(转)

    测试过程中有时候需要生成大量的假数据,faker 是一个生成假数据的类库,可以生成姓名,电话,IP地址,密码,ISBN等等你能想到的或者你想不到的各种类型的假数据. Yii2.0已经集成该类库,不用再 ...

  6. 关于 IOS code signe 和 Provisioning Files 机制 浅析

    可以先读下这个译文. http://www.cnblogs.com/zilongshanren/archive/2011/08/30/2159086.html 读后,有以下疑惑. 在mac 机上生成的 ...

  7. 一种解决新版本API完全兼容老版本API的方法

    原文:http://android.eoe.cn/topic/android_sdk 这节课程我们讨论如何创建一个实现类,即能对应新版本的API,又能够保持对老版本API的支持. * 寻找一个替代的解 ...

  8. linux经常使用命令-帮助命令-授之以渔

    原创Blog,转载请注明出处 http://blog.csdn.net/hello_hwc 我的虚拟机系统是CentOS.版本号较老,谅解 一.为什么要学习帮助命令?   授人以鱼不如授人以渔.学会了 ...

  9. SVN不显示对号的解决方法

    具体操作方法如下: 1.到C:\Windows文件夹下,打开regedit.exe 2.Ctrl+F,搜索“ShellIconOverlayIdentifiers” 3.将TortoiseAdded. ...

  10. jquery JSON的解析方式实例分享

    本文以jquery异步获取的数据类型——json对象和字符串为依据,介绍两种方式获取到的结果处理方式. 这里考虑都考虑的是服务器返回的是JSON形式的字符串的形式,对于利用JSONObject等插件封 ...