`
gwh_08
  • 浏览: 332105 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

逗号分割的行数据变列

 
阅读更多

mysql数据表中数据如下:

id	result
47857	2,13,16
47872	10,13,16
47875	16,13,10
47970	1,16,19

 要把逗号分割的值,以列的形式查询出来,处理方式如下:

	SELECT a.id, 
		substring_index( substring_index( a.result, ',', b.help_topic_id + 1 ), ',',- 1 ) result 
	FROM t_table a
	LEFT JOIN mysql.help_topic b ON b.help_topic_id < ( length( a.result ) - length( REPLACE ( a.result, ',', '' ) ) + 1 )

 查询结果如下:

id	result
47857	2
47857	13
47857	16
47872	10
47872	13
47872	16
47875	16
47875	13
47875	10
47970	1
47970	16
47970	19

 其中,关联表mysql.help_topic  是为了利用他的id(从1开始的自增长id)自增长的特性来对分割后的列进行行数控制,其id最大值为700以上,基本上可以满足行被拆分的数量要求。

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics