目录
-
行转列
行转列的两种方法:(1)case when (2)IF()
select
student_id
#case when, sum()/avg()/max()/min()都可以
, sum(case when subject = '数学' then score end) as '数学'
,sum(case when subject = '英语' then score end) as '英语'
, sum((case when subject = '语文' then score end) as '语文'
#if()
,sum(if(subject='数学',score,0)) '数学'
from table1
group by student_id
-
列转行
使用union all或者union;union去重
select student_id,'数学' as subject,数学 as score
from table2
union all
select
from table2
union all
select
from table2