mysql列转行仅适用于复杂逻辑sql脚本

select substring_index(
	substring_index(t1.ori_comma_values, ',', t2.split_times), ',', -1) as split_value
	from (select '13055,28917,33583,42776,43832,' ori_comma_values) t1
	inner join (
		-- 无实际意义,用于兼容被转义列中实际数据的个数,这里认为列中有1000个有效数据 可以无限大,用于兜底
		select (@i := @i + 1) as split_times from t_table01 t join (select @i := 0) n limit 1000
	) t2 
	-- 原数据字符长度 - 去掉了分割字符的数据字符长度 = 得到实际有效数据个数
	on char_length(t1.ori_comma_values) - char_length(replace (t1.ori_comma_values, ',', '')) >= t2.split_times - 1

posted @ 2024-05-17 15:25  Journey&Flower  阅读(11)  评论(0)    收藏  举报