MYSQL中列转行的方法

初始表:temp_count

目标表:machine_count

利用max(case when then)

select temp_count.id,
 MAX(CASE temp_count.`故障部位` WHEN '动力' THEN temp_count.`故障次数` ELSE 0 END ) '动力',
 MAX(CASE temp_count.`故障部位` WHEN '液压系统' THEN temp_count.`故障次数` ELSE 0 END ) '液压系统',
 MAX(CASE temp_count.`故障部位` WHEN '电装品' THEN temp_count.`故障次数` ELSE 0 END ) '电装品'
 from temp_count
GROUP BY temp_count.id;

猜你喜欢

转载自blog.csdn.net/Griezmann_7/article/details/86010404