MYSQL中竖表转横表

表结构
MYSQL中竖表转横表_第1张图片

select goods_id,
sum( case field_code when ‘watch_count’ then field_value else 0 end ) watch_count,
sum( case field_code when ‘favorite_count’ then field_value else 0 end ) favorite_count,
sum( case field_code when ‘comment_count’ then field_value else 0 end ) comment_count,
sum( case field_code when ‘praise_count’ then field_value else 0 end ) praise_count
from tbl_good group by goods_id

MYSQL中竖表转横表_第2张图片

你可能感兴趣的:(mysql)