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. Swift 可选型

    1.可选型 Swift 语言为我们提供了一种全新的.更加安全的类型 "可选型".可选型是使用范型枚举的形式来组织的,也就是说此特性可以运用于所有的类型.结构体.类或者其他复杂数据类 ...

  2. java.lang.IllegalStateException: No typehandler found for mapping XXX

    前言:今天遇到了这个问题,刚开始觉得很容易解决的,毕竟能定位到出问题的文件和对应的字段,根据以往的经验也可以判断出是字段映射类型不匹配的问题,后来找了半天还是没找到问题的根源,从网上百度,也没看到令人 ...

  3. [译]async/await中使用阻塞式代码导致死锁 百万数据排序:优化的选择排序(堆排序)

    [译]async/await中使用阻塞式代码导致死锁 这篇博文主要是讲解在async/await中使用阻塞式代码导致死锁的问题,以及如何避免出现这种死锁.内容主要是从作者Stephen Cleary的 ...

  4. Bitnami Redmine 中文附件名 报错修复

    最近自己在服务器上搭了个redmine,用的是Bitnami的一键安装程序. 搭好后,运行得不错,居然还增加了负载均衡. 某天上传中文附件,打开报内部错误,去redmine官网看了下,果然有这个问题, ...

  5. Leetcode 编程训练

    Leetcode这个网站上的题都是一些经典的公司用来面试应聘者的面试题,很多人通过刷这些题来应聘一些喜欢面试算法的公司,比如:Google.微软.Facebook.Amazon之类的这些公司,基本上是 ...

  6. SLA等级那些9的实际意义

    1. 重要的系统起码要设计达到99.9%的可靠性吧. 俗称3个9,这是什么意思呢? (1-99.9%)*365*24=8.76小时,表示该软件系统在连续运行1年时间里最多可能的业务中断时间是8.76小 ...

  7. 【转载】Spring Cache介绍

    原文地址:http://www.cnblogs.com/rollenholt/p/4202631.html 缓存是实际工作中非常常用的一种提高性能的方法, 我们会在许多场景下来使用缓存. 本文通过一个 ...

  8. CentOS7.2安装pure-ftpd 及其配置项

    CentOS7.2安装FTP(pure-ftpd-1.0.43) 原文链接: https://www.linuxidc.com/Linux/2016-10/135971.htm [日期:2016-10 ...

  9. python中decode和encode的区别

    #-*-coding:utf-8 import sys ''' *首先要搞清楚,字符串在Python内部的表示是unicode编码,因此,在做编码转换时,通常需要以unicode作为中间编码, 即先将 ...

  10. 解决 p0sixspwn-v1.0.4 win版 无法定位程序输入点sqlite3_wal_checkpoint的问题

    p0sixspwn-v1.0.4 win版今天早晨发现大神( @winocm · @iH8sn0w · @SquiffyPwn) 已经发布. 下载下来运行之,发现会报错: 无法定位程序输入点sqlit ...