mysql行转列转换
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行转列转换的更多相关文章
- MySQL 行转列 -》动态行转列 -》动态行转列带计算
Pivot Table Using MySQL - A Complete Guide | WebDevZoomhttp://webdevzoom.com/pivot-table-using-mysql ...
- MYSQL 行转列 以及基本的聚合函数count,与group by 以及distinct组合使用
在统计查询中,经常会用到count函数,这里是基础的 MYSQL 行转列 以及基本的聚合函数count,与group by 以及distinct组合使用 -- 创建表 CREATE TABLE `tb ...
- mysql行转列 问题 SUM(IF(条件,列值,0))
sum(if(条件,列值,0))语法用例: select name,sum(if(subject="语文",score,0)) as "语文" from gra ...
- mysql行转列、列转行示例
最近在开发过程中遇到问题,需要将数据库中一张表信息进行行转列操作,再将每列(即每个字段)作为与其他表进行联表查询的字段进行显示. 借此机会,在网上查阅了相关方法,现总结出一种比较简单易懂的方法备用. ...
- MySql 行转列 存储过程实现
同学们在使用mysql的过程中,会遇到一个行转列的问题,就是把多条数据转化成一条数据 用多列显示. 方法1. 实现方式用下面的存储过程,表名对应的修改就行. BEGIN declare current ...
- mysql 行转列 列转行
一.行转列 即将原本同一列下多行的不同内容作为多个字段,输出对应内容. 建表语句 DROP TABLE IF EXISTS tb_score; CREATE TABLE tb_score( id ) ...
- MySQL行转列与列转行
行转列 例如:把图1转换成图2结果展示 图1 图2 CREATE TABLE `TEST_TB_GRADE` ( `ID` ) NOT NULL AUTO_INCREMENT, `) DEFAULT ...
- [转]mysql 行转列 列转行
原文地址:http://www.cnblogs.com/xiaoxi/p/7151433.html 一.行转列 即将原本同一列下多行的不同内容作为多个字段,输出对应内容. 建表语句 DROP TABL ...
- mysql行转列,单列转多行
行转列 使用CASE语句: SELECT SUM(CASE USER_NAME='A' THEN KILLS END) AS 'A', SUM(CASE USERNAME='B' THEN KILL ...
随机推荐
- block(九)Block 和 Delegate 的使用比较
Block 和 Delegate中的方法都可以理解成回调函数,当某件事情发生的时候取执行一段代码片段 Block(代码块) 优点:是一种轻量级的回调,能够直接访问上下文,使用块的地方和块的实现地方在同 ...
- Bitnami Redmine 中文附件名 报错修复
最近自己在服务器上搭了个redmine,用的是Bitnami的一键安装程序. 搭好后,运行得不错,居然还增加了负载均衡. 某天上传中文附件,打开报内部错误,去redmine官网看了下,果然有这个问题, ...
- ListView点击Item展开隐藏项(单项展开、多项展开、复杂布局时的展开处理)
手机屏幕毕竟有限,当我们要显示较多数据时便不得不舍去一些次要信息.将主要信息优先显示,也使显示效果更加简洁美观.遇到类似的需求,我们使用最多的就是 ListView ,而假设每次点击一个 Item 都 ...
- xml中的<![CDATA[]]> 简介
被<![CDATA[]]>这个标记所包含的内容将表示为纯文本,比如<![CDATA[<]]>表示文本内容“<”. 此标记用于xml文档中,我们先来看看使用转义符的 ...
- 一步一步学android之控件篇——ListView基本使用
ListView组件在应用程序中可以说是不可或缺的一部分,ListView主要是显示列表数据,同时可以滚动查看,这篇博客主要是对ListView的基本用法进行说明,后面会依次对ListView点击动态 ...
- 神经网络写诗(charRNN)
https://github.com/chenyuntc/pytorch-book 基于pytorch ,许多有趣的小应用.感谢作者! 作者的代码写得非常清晰,配置方法也很明确,只需要按照提示,安装依 ...
- django -- 为模式增加方法
在django中模式中的方法是行级的.也就是说它操作是表里的行.不是整个表 一.模式定义: from django.db import models # Create your models here ...
- php7+apache2.4 (Windows7下)安装
条件: ( 电脑必须win7 sp1, .netframework4 ) 一.下载php7和apache2.4 首先下载php7的windows压缩包,到这里下载http://windows.php. ...
- 随便浏览感觉简单易用的Orm
https://www.cnblogs.com/babietongtianta/p/4365195.html CYQ ITDOS CHOLE.ORM
- 猫猫学iOS之小知识之xcode6自己主动提示图片插件 KSImageNamed的安装
猫猫分享,必须精品 原创文章,欢迎转载.转载请注明:翟乃玉的博客 地址:http://blog.csdn.net/u013357243 一:首先看效果 KSImageNamed是让XCode能预览项目 ...