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. MVC+Spring.NET+NHibernate .NET SSH框架整合 C# 委托异步 和 async /await 两种实现的异步 如何消除点击按钮时周围出现的白线? Linq中 AsQueryable(), AsEnumerable()和ToList()的区别和用法

    MVC+Spring.NET+NHibernate .NET SSH框架整合   在JAVA中,SSH框架可谓是无人不晓,就和.NET中的MVC框架一样普及.作为一个初学者,可以感受到.NET出了MV ...

  2. rdlc 分页操作和分页统计

    1. 工具箱中拖一个列表过来,设置 列表-->行组-->组属性常规-->组表达式=Int((RowNumber(Nothing)-1)/10)分页符-->勾选在组的结尾. 2. ...

  3. Spring JavaConfig

    以前,Spring推荐使用XML的方式来定义Bean及Bean之间的装配规则,但是在Spring3.0之后,Spring提出的强大的JavaConfig这种类型安全的Bean装配方式,它基于Java代 ...

  4. es5 温故而知新 简单继承示例

    // 矩形(构造器/父类) function Rectangle (height, width) { this.height = height; this.width = width; } // 获取 ...

  5. shiro 基本认知

    1 shiro 的作用:安全.权限管理. 具有的功能:认证.授权.加密.会话管理.web集成.缓存 2 shiro 结构 3

  6. 更改MySQL数据库目录位置[zz]

    MYSQL默认的数据文件存储目录为/var/lib/mysql.假如要把目录移到/home/data下需要进行下面几步:1.home目录下建立data目录cd /homemkdir data2.把My ...

  7. 关于Server Tomcat v8.0 Server at localhost failed to start的解决办法

    测试环境: Eclipse Java EE IDE for Web Developers. Version: Luna Service Release 1 (4.4.1)Build id: 20140 ...

  8. C++ smart pointer智能指针

      在C++中,程序员可以直接操作内存,给编程增加了不少的灵活性.但是灵活性是有代价的,程序员必须负责自己负责释放自己申请的内存,否则就会出现内存泄露.智能指针就是为了解决这个问题而存在的.它和其他指 ...

  9. Spring Security教程(八):用户认证流程源码详解

    本篇文章主要围绕下面几个问题来深入源码: 用户认证流程 认证结果如何在多个请求之间共享 获取认证用户信息 一.用户认证流程 上节中提到Spring Security核心就是一系列的过滤器链,当一个请求 ...

  10. 微信小程序-wx:for 循环列表

      获取了 N 条信息(具体有多少条不确定),如何在界面中动态呈现出来呢? .wxml 代码 <view wx:for="{{items}}" wx:for-index=&q ...