场景:查询出来一条数据,其中fee_items字段为json数组,现在要把json数组拆分,如果有多条,则展示多行数据,列转行

表中的数据 字段 fee_items 是JSON数组、查询出来是一条,现在要拆分数组,如果多条展示多行

其中发现列转行的时候发现有空格,其中还去除了空格

SELECT 
	t.id,t.detail_id,t.activity_sn,
  SUBSTRING_INDEX( SUBSTRING_INDEX( t.feeId, ',', b.help_topic_id + 1 ), ',',- 1 ) AS feeId ,
  SUBSTRING_INDEX( SUBSTRING_INDEX( t.quantity, ',', b.help_topic_id + 1 ), ',',- 1 ) AS quantity ,
  SUBSTRING_INDEX( SUBSTRING_INDEX( t.feeAmount, ',', b.help_topic_id + 1 ), ',',- 1 ) AS feeAmount ,
  SUBSTRING_INDEX( SUBSTRING_INDEX( t.feeCode, ',', b.help_topic_id + 1 ), ',',- 1 ) AS feeCode ,
  SUBSTRING_INDEX( SUBSTRING_INDEX( t.feeName, ',', b.help_topic_id + 1 ), ',',- 1 ) AS feeName ,
  t.fee_items
 from (

	SELECT 
	*,
	replace(TRIM(BOTH ']' FROM TRIM(LEADING '[' FROM REPLACE((JSON_EXTRACT (fee_items, '$[*].id')),'"',''))),' ','') as feeId,
  replace(TRIM(BOTH ']' FROM TRIM(LEADING '[' FROM REPLACE((JSON_EXTRACT (fee_items, '$[*].quantity')),'"',''))),' ','') as quantity,
  replace(TRIM(BOTH ']' FROM TRIM(LEADING '[' FROM REPLACE((JSON_EXTRACT (fee_items, '$[*].feeAmount')),'"',''))),' ','') as feeAmount,
  replace(TRIM(BOTH ']' FROM TRIM(LEADING '[' FROM REPLACE((JSON_EXTRACT (fee_items, '$[*].feeCode')),'"',''))),' ','') as feeCode,
  replace(TRIM(BOTH ']' FROM TRIM(LEADING '[' FROM REPLACE((JSON_EXTRACT (fee_items, '$[*].feeName')),'"',''))),' ','') as feeName
FROM
	表
WHERE
	id = 'atc717072872866054145' 
)t
JOIN mysql.help_topic AS b ON b.help_topic_id < ( length( t.feeId ) - length( REPLACE ( t.feeId, ',', '' ) ) + 1 );

GitHub 加速计划 / js / json
41.72 K
6.61 K
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:1 个月前 )
960b763e 4 个月前
8c391e04 6 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐