gpt4 book ai didi

mysql - 在 MySQL 中,如何拆分每个分隔多个值的分隔字符串

转载 作者:行者123 更新时间:2023-11-29 23:16:21 27 4
gpt4 key购买 nike

假设我将此字符串传递给我的存储过程: '1,10;2,11;3,12'我需要将其拆分出来,以便可以插入到如下表中:

   ID   VALUE
1 10
2 11
3 12

我可以使用什么 MySQL 代码来分割字符串并插入到表中,如图所示?谢谢!

最佳答案

您可以使用如下查询来分解字符串:

select substring_index(substring_index(substring_index('1,10;2,11;3,12', ';', n.n), ';', -1), ',', 1) as id,
substring_index(substring_index(substring_index('1,10;2,11;3,12', ';', n.n), ';', -1), ',', 1) as value
from (select 1 as n union all select 2 union all select 3) n;

您可以在此之前将insert into t(id, value) 进行插入

关于mysql - 在 MySQL 中,如何拆分每个分隔多个值的分隔字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27761452/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com