工作时候前辈教的mysql数据转置的一种方法:
select * from p_test;
select '2019年' as 年份,
max(case when p_type = '统计1' then `2019年` end) as 统计1 ,
max(case when p_type = '统计2' then `2019年` end) as 统计2 ,
max(case when p_type = '统计3' then `2019年` end) as 统计3
from p_test
union all
select '2020年',
max(case when p_type = '统计1' then `2020年` end) as 统计1 ,
max(case when p_type = '统计2' then `2020年` end) as 统计2 ,
max(case when p_type = '统计3' then `2020年` end) as 统计3
from p_test
union ALL
select '2021年',
max(case when p_type = '统计1' then `2021年` end) as 统计1 ,
max(case when p_type = '统计2' then `2021年` end) as 统计2 ,
max(case when p_type = '统计3' then `2021年` end) as 统计3
from p_test;
MySQL实现数据转置(行转列,列转行)
最新推荐文章于 2024-04-06 20:56:42 发布