漫学笔记之mysql 行转列
来源:互联网 发布:mad建筑 知乎 编辑:程序博客网 时间:2024/04/30 05:36
以下是测试数据
CREATE TABLE student (
stuid VARCHAR(16) NOT NULL COMMENT '学号',
stunm VARCHAR(20) NOT NULL COMMENT '学生姓名',
PRIMARY KEY (stuid)
);
Insert Into student Values
('1001', '西施'),
('1002', '王昭君'),
('1003', '貂蝉');
CREATE TABLE courses (
courseid VARCHAR(20) NOT NULL comment '课程编号',
coursenm VARCHAR(100) NOT NULL comment '课程名称',
PRIMARY KEY (courseid)
)COMMENT='课程表';
insert into courses VALUES
('c1001','琴'),
('c1002','棋'),
('c1003','书'),
('c1004','画');
CREATE TABLE score (
scid varchar(16) not NULL comment'成绩编号',
stuid VARCHAR(16) NOT NULL comment'学生编号',
courseid VARCHAR(20) NOT NULL comment'课程编号',
scores FLOAT NULL DEFAULT NULL comment'成绩',
PRIMARY KEY (scid)
);
insert into score values
('1','1001','c1001',80),
('2','1001','c1002',90),
('3','1001','c1003',82),
('4','1001','c1004',83),
('5','1002','c1001',83),
('6','1002','c1002',90),
('7','1002','c1003',85),
('8','1002','c1004',86),
('9','1003','c1001',90),
('10','1003','c1002',89),
('11','1003','c1003',82),
('12','1003','c1004',88);
select score.stuid ,stunm , coursenm,scores from score
inner join student on student.stuid = score.stuid
inner join courses on courses.courseid = score.courseid order by stuid , scores ;
查询结果:
行转列 1 使用max函数
select score.stuid,stunm,
max( case WHEN courses.coursenm = '琴' then ifnull(score.scores,0) end ) '琴' ,
max( case WHEN courses.coursenm = '棋' then ifnull(score.scores,0) end ) '棋' ,
max( case WHEN courses.coursenm = '书' then ifnull(score.scores,0) end ) '书' ,
max( case WHEN courses.coursenm = '画' then ifnull(score.scores,0) end ) '画'
from score
inner join courses on courses.courseid = score.courseid
inner join student on student.stuid = score.stuid
group by score.stuid;
结果:
行转列 2 使用sum函数
select score.courseid 课程编号,courses.coursenm 课程名称,
sum( case WHEN student.stunm = '西施' then ifnull(score.scores,0) end ) '西施' ,
sum( case WHEN student.stunm = '王昭君' then ifnull(score.scores,0) end ) '王昭君' ,
sum( case WHEN student.stunm = '貂蝉' then ifnull(score.scores,0) end ) '貂蝉'
from score
inner join courses on courses.courseid = score.courseid
inner join student on student.stuid = score.stuid
group by score.courseid;
结果:
列传行 新建表并 插入数据
create table result (
stuid varchar(16) not null comment'学生编号',
stunm varchar(20) not null comment'学生姓名',
qin FLOAT NULL DEFAULT NULL comment'琴成绩',
qi FLOAT NULL DEFAULT NULL comment'棋成绩',
shu FLOAT NULL DEFAULT NULL comment'书成绩',
hua FLOAT NULL DEFAULT NULL comment'画成绩'
);
insert into result VALUES
('1001','西施',80,90,82,83),
('1002','王昭君',83,90,85,86),
('1003','貂蝉',90,90,82,88);
select * from result
查询结果:
列转行 union
UNION 用于合并两个或多个 SELECT 语句的结果集。
select stuid , stunm , '琴' 课程 , qin as 成绩 from result
union
select stuid , stunm , '棋' 课程 , qi as 成绩 from result
union
select stuid , stunm , '书' 课程 , shu as 成绩 from result
union
select stuid , stunm , '画' 课程 , hua as 成绩 from result
order by stuid , 课程
查询结果:
- 漫学笔记之mysql 行转列
- 漫学笔记之Apache虚拟主机配置
- MySQL再学笔记_00
- 读数学之美笔记
- (2)Mysql ---- 索引ABC 《MYSQL 5.5从零开始学》笔记
- 重学PHP笔记之 Filesystem Functions
- 《看日记学git》之随笔笔记
- <一>新手学opencv之cvSetMouseCallback笔记
- <二>新手学opencv之cvCreateTrackbar笔记
- 快学scala笔记之01-数据类型
- 快学scala笔记之02-类
- 学习笔记之MySQL
- Mysql笔记之 数据类型
- MySQL常用操作(笔记比较乱学完整理)
- 菜鸟学前端之遍寻名师之混乱的笔记
- [jjzhu学java]之solr4.9同步mysql数据
- 从零开始学网络爬虫之数据区MySQL
- MySQL笔记之开发篇
- 游戏引擎研究 —— 单位向量与八面体的转换算法
- Storage knowledge
- Windows Nginx 配置多域名多Tomcat转发
- linux编译内核
- 使用applicationIdSuffix打多个包
- 漫学笔记之mysql 行转列
- Stylish样式——知乎
- Linux--守护进程
- ROS机器人Diego 1#制作(二十三)搭载EAI F4激光雷达move_base路径规划
- linux虚拟机网络IP地址设置
- 基础线程模板
- MFC9.0 更改窗口的初始化大小位置初探 (PreCreateWindow无效)
- C-C++语言结构体中定义另一个结构体指针的经验
- document.ready和onload的区别