mysql 行列转换

2020-06-08 14:31:07 浏览数 (1)

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

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
sum

0 人点赞