MySQL行转列写法注意 case when 要不要加else
select * from test ;
想把num1列的行转成列
select SUM(CASE num1 WHEN 1 THEN num2 END), SUM(CASE num1 WHEN 3 THEN num2 END) from test ;
如果case when 后加了else 0
select SUM(CASE num1 WHEN 1 THEN num2 END), SUM(CASE num1 WHEN 3 THEN num2 else 0 END),SUM(CASE num1 WHEN 5 THEN num2 else 0 END),SUM(CASE num1 WHEN 7 THEN num2 END) from test ;
case when 后加了else 0num1没有匹配的行结果显示0,如果没加结果是null