Mysql行转列的实现
来源:互联网 发布:京东商城与淘宝的区别 编辑:程序博客网 时间:2024/05/17 07:00
应用场景:以学生为单位,查询出每个学生对应的每个科目的考试成绩。
创建示例数据库表
create table `scores` ( `id` int(11) not null auto_increment, `user_id` int(11) default null comment '用户id', `subject` varchar(100) default null comment '学科', `score` decimal(5,2) default null comment '分数', primary key (`id`)) engine=innodb default charset=utf8;插入示例数据
insert into `scores` values (1, 1, '哲学', 92);insert into `scores` values (2, 1, '英语', 71);insert into `scores` values (3, 1, '体育', 59);insert into `scores` values (4, 1, '网络技术', 86);insert into `scores` values (5, 1, '数据结构与算法', 93);insert into `scores` values (6, 2, '哲学', 69);insert into `scores` values (7, 2, '英语', 87);insert into `scores` values (8, 2, '体育', 90);insert into `scores` values (9, 2, '网络技术', 53);insert into `scores` values (10, 2, '数据结构与算法', 51);insert into `scores` values (11, 3, '哲学', 70);insert into `scores` values (12, 3, '英语', 63);insert into `scores` values (13, 3, '体育', 79);insert into `scores` values (14, 3, '网络技术', 80);insert into `scores` values (15, 3, '数据结构与算法', 81);正常查询
select * from scores;查询结果:
行转列查询
select user_id,max(case `subject` when '哲学' then score else 0 end) as '哲学',max(case `subject` when '英语' then score else 0 end) as '英语',max(case `subject` when '体育' then score else 0 end) as '体育',max(case `subject` when '网络技术' then score else 0 end) as '网络技术',max(case `subject` when '数据结构与算法' then score else 0 end) as '数据结构与算法'from scoresgroup by user_id;查询结果:
0 0
- mysql 行转列的实现
- Mysql行转列的实现
- mysql实现行转列的两个方式
- Mysql的transaction实现
- MySQL的ROWNUM实现
- mysql索引的实现
- MySQL分页的实现
- MySQL 的 RowNum 实现
- MySQL 的 RowNum 实现
- mysql的索引实现
- MySQL 的 RowNum 实现
- MySQL 的 RowNum 实现
- MySQL 的 RowNum 实现
- MySQL 的 RowNum 实现
- MySQL 的 RowNum 实现
- MySQL OSC的实现
- MySQL行号的实现
- mysql rownum的实现
- 工作——自动化测试中用到的SQL server查询语句的格式调整
- 65.Which two statements are true about checkpointing? (Choose two.)
- Ubuntu 配置串口信息
- 【转】console.log 用法
- 带有两层和三层列表的ExpandableListView
- Mysql行转列的实现
- 前后端分离项目实践分析
- libsvm使用教程
- [编写高质量iOS代码的52个有效方法](三)消息和运行期
- 关于文字环绕图片的问题
- 'xxx' declared `static' but never defined
- Bellovin(最长上升子序列)
- iOS开发封装篇-利用KVC修改UITextField的placeholderLabel颜色
- session和cookie