首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >mysql 行列转换

mysql 行列转换

作者头像
崔笑颜
发布于 2020-06-08 14:31:07
发布于 2020-06-08 14:31:07
2.4K0
举报

需求是将以下两种表样式互相转换:

score_1转score_2,如下: 这里也可以用来统计数据

如果等于1 就加一 不然就不加 SUM(IF(C.question_type = '1', 1, 0)) AS '论述'

代码语言:javascript
复制
select ID
    ,max(case when course='数学' then score end) as '数学'
    ,max(case when course='英语' then score end) as '英语'
    ,max(case when course='法律' then score end) as '法律'
from score_1
group by id;

score_2转score_1:两种引号不同

代码语言:javascript
复制
select id 
    ,'数学' as course
    ,`数学` as score
from score_2
union all 
select id 
    ,'英语' as course
    ,`英语` as score
from score_2
union all 
select id 
    ,'法律' as course
    ,`法律` as score
from score_2
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-08-21,如有侵权请联系 cloudcommunity@tencent.com 删除
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档