mysql横表纵表转换

一.横表转纵表数学

select id,姓名,'语文' as SUBJECT,语文 as 'score' from hb 
UNION
select id,姓名,'数学' as SUBJECT,数学 as 'score' from hb 
UNION
select id,姓名,'英语' as SUBJECT,英语 as 'score' from hb select

二.纵表转横表im

select name as '姓名',MAX(CASE `subject` WHEN '语文' then score else 0 end) 语文,
MAX(CASE `subject` WHEN '数学' then score else 0 end) 数学,
MAX(CASE `subject` WHEN '英语' then score else 0 end) 英语 from sb group by nameimg

相关文章
相关标签/搜索