gpt4 book ai didi

Mysql通过分隔符将字符串(列)分割成新行

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

我有以下字符串156,12,99,12,14,17,有时它会更长和/或更短,并且涉及字母和数字,例如 cc,21,366,ddd。

理想的输出是 output :

output

我有这个给我列中的分隔符数量:

select
LENGTH(a.column) - LENGTH(REPLACE(a.column, ',', '')) + 1 AS numberofdelimit
FROM <mydatabase>.<mytable> as a
where a.id = 4;

我也知道这段代码会给我分隔符处的文本:

SUBSTRING_INDEX(a.column, ',', numbers.n)

我正在努力将其组合在一起以将值放入新行中。

最佳答案

好吧,如果您不再需要 ID,您可以执行以下操作:

create table `ids` (
`id` int
);

create table `input` (
str varchar(256)
);

insert into ids (`id`) values
(1), (2), (3), (4), (10), (20), (30), (40);

insert into input (`str`) values ('1,2,10,30');

然后像这样匹配

select
*
from
input inner join ids
on concat(',',str,',') rlike concat(',', id, ',');

不过,它确实需要您在一侧有一张 table 。

但这可能是一个开始。

关于Mysql通过分隔符将字符串(列)分割成新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35679234/

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