首页 > 代码库 > Mysql 行转列

Mysql 行转列


select
* from t_zg_asset_statistic_info a where a.BATCH_NO=ss20170123181418 ;

这是原始数据:

技术分享

这是行转列后的样子:

select a.DEBT_TYPE, sum(case  WHEN DEBT_TYPE =0  THEN AMOUNT END) 初债,sum(case  WHEN DEBT_TYPE =1  THEN AMOUNT END) 二级债 , CONVERT(group_concat(DEBT_TYPE,总量:, AMOUNT) USING utf8) as 拼接合计,sum(AMOUNT) as 合计  from  t_zg_asset_statistic_info a where a.BATCH_NO=ss20170123181418GROUP BY  a.DEBT_TYPEORDER BY a.DEBT_TYPE;

技术分享

后记:mysql  BLOB字段转String的方法

CONVERT(group_concat(DEBT_TYPE,总量:, AMOUNT) USING utf8)

 

Mysql 行转列