当前位置: 代码网 > it编程>数据库>Mysql > Mysql行转列把逗号分隔的字段拆分成多行两种方法

Mysql行转列把逗号分隔的字段拆分成多行两种方法

2024年06月10日 Mysql 我要评论
效果如下源数据变更后的数据方法第一种先执行下面的sql,看不看能不能执行,如果有结果,代表数据库版本是可以的,可以看下面和自己表关联的sql,如果不行用第二种。示例sqlselect substrin

效果如下

源数据

变更后的数据

方法

第一种

先执行下面的sql,看不看能不能执行,如果有结果,代表数据库版本是可以的,可以看下面和自己表关联的sql,如果不行用第二种。

示例sql

select substring_index(substring_index('7654,7698,7782,7788',',',help_topic_id+1),',',-1) as num 
from mysql.help_topic 
where help_topic_id < length('7654,7698,7782,7788')-length(replace('7654,7698,7782,7788',',',''))+1

和业务结合在一起使用

select
		a.store_signer_name,
		substring_index( substring_index( a.concatstoreid, ',', b.help_topic_id + 1 ), ',', - 1 ) as concatstoreid 
	from
		(select store_signer_nameconcatstoreid from test) a
		inner join mysql.help_topic b on b.help_topic_id < (
			length( a.concatstoreid ) - length(
			replace ( a.concatstoreid, ',', '' )) + 1)

其核心在于mysql.help_topic,但是版本太低的数据库版本不支持,如果不支持,可以用下面第二种。

第二种

示例sql

select
  substring_index(substring_index(table_name.csv_values, ',', numbers.n), ',', -1) as split_value
from
  table_name
  inner join
  (select 1 n union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9 union all select 10 union all
   select 11 union all select 12 union all select 13 union all select 14 union all select 15 union all select 16 union all select 17 union all select 18 union all select 19 union all
   select 20 union all select 21 union all select 22 union all select 23 union all select 24 union all select 25 union all select 26 union all select 27 union all select 28 union all
   select 29 union all select 30 union all select 31 union all select 32 union all select 33 union all select 34 union all select 35 union all select 36 union all select 37 union all
   select 38 union all select 39 union all select 40) numbers
  on char_length(table_name.csv_values) - char_length(replace(table_name.csv_values, ',', '')) >= numbers.n - 1;

在上面的查询中,因为我逗号分隔的最大个数是36,所以我添加了40个union all select子句,以生成数字序列1到40。你可以根据需要调整这个序列的长度。

请注意,如果你的逗号分隔值个数大于40,那么你需要相应地增加数字序列的长度。

和业务结合在一起使用

select
table_name.store_signer_name,
table_name.store_signer_contact,
  substring_index(substring_index(table_name.concatstoreid, ',', numbers.n), ',', -1) as store_id
from
  (select store_signer_name,store_signer_contact,group_concat(store_id) concatstoreid from t_store_esgin_info where business_status = 1003 and type =0 and start_year = 2023  group by store_signer_name,store_signer_contact having count(1) > 1) table_name
  inner join
  (select 1 n union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9 union all select 10 union all
   select 11 union all select 12 union all select 13 union all select 14 union all select 15 union all select 16 union all select 17 union all select 18 union all select 19 union all
   select 20 union all select 21 union all select 22 union all select 23 union all select 24 union all select 25 union all select 26 union all select 27 union all select 28 union all
   select 29 union all select 30 union all select 31 union all select 32 union all select 33 union all select 34 union all select 35 union all select 36 union all select 37 union all
   select 38 union all select 39 union all select 40) numbers
  on char_length(table_name.concatstoreid) - char_length(replace(table_name.concatstoreid, ',', '')) >= numbers.n - 1;

结论

如果mysql版本较低,使用第二种,如果可以执行第一种示例sql,那么推荐使用第一种,动态的。

到此这篇关于mysql行转列把逗号分隔的字段拆分成多行的两种方法的文章就介绍到这了,更多相关mysql逗号分隔字段拆分多行内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com