MySQL的行列转换
来源:互联网 发布:李小龙身体实际数据 编辑:程序博客网 时间:2024/05/02 02:21
MySQL的行列转换
MySQL的行列转换的写法, 如果有需要可以参考下
现在如下有两个表:
成绩表(t_score)
科目表(t_subject)
想要转成如下的表:
中间代码
select CONCAT(" sum(case when j.`subject`='",`subject`,"' then s.score end) as '",`subject`,"',") as tempcodefrom t_subject
查询结果
subject
=’语文’ then s.score end) as ‘语文’, sum(case when j.subject
=’数学’ then s.score end) as ‘数学’, sum(case when j.subject
=’英语’ then s.score end) as ‘英语’, sum(case when j.subject
=’物理’ then s.score end) as ‘物理’, sum(case when j.subject
=’化学’ then s.score end) as ‘化学’, sum(case when j.subject
=’生物’ then s.score end) as ‘生物’, sum(case when j.subject
=’政治’ then s.score end) as ‘政治’, sum(case when j.subject
=’历史’ then s.score end) as ‘历史’, sum(case when j.subject
=’地理’ then s.score end) as ‘地理’,最终转换代码
select s.`id`, sum(case when j.`subject`='语文' then s.score end) as '语文', sum(case when j.`subject`='数学' then s.score end) as '数学', sum(case when j.`subject`='英语' then s.score end) as '英语', sum(case when j.`subject`='物理' then s.score end) as '物理', sum(case when j.`subject`='化学' then s.score end) as '化学', sum(case when j.`subject`='生物' then s.score end) as '生物', sum(case when j.`subject`='政治' then s.score end) as '政治', sum(case when j.`subject`='历史' then s.score end) as '历史', sum(case when j.`subject`='地理' then s.score end) as '地理'from t_score s inner join t_subject j on s.subjectid=j.`id`
查询结果就是上面的结果, 就不再贴出了.
0 0
- MySQL的行列转换
- mysql的行列转换
- 关于MYSQL的行列转换
- MySQL逗号分割字段的行列转换
- Mysql行列转换
- mysql行列转换
- Mysql 行列转换
- mysql行列转换示例
- mysql 行列转换
- mysql 行列转换方式
- Mysql行列转换
- MySQL 行列转换
- Mysql-行列转换
- mysql行列转换
- mysql行列转换
- mysql行列转换
- mysql行列转换
- mysql行列转换方法总结
- 相关性 把excel数据导入数据库 2016.08.02回顾
- mysql优化
- CodeForces 595B Pasha and Phone
- Gradle之android配置
- visual studio 2015 升级为 up3后测试资源管理器空白的现象(已修复)
- MySQL的行列转换
- 【R】数据缺失值处理
- JSP 指令
- 背包问题
- iOS8系统H264视频硬件编解码说明
- Mediaplayer和surfaceview播放录制视频
- C语言函数sscanf()的用法
- JSON-lib的使用
- Linux关于ctime,mtime,atime的意义