在 MySQL 8 中使用 sProc 拆分由分号分隔的字符串值
Split string values separated by a semicolon using sProc in MySQL 8
我需要在 MySQL
8 版本
中使用 stored Procedure
拆分此字符串
"John;Elizabeth;Mark;Zagor;Annie;Lucy;Peter;Robin;Wilson;Tom;Bettie;Myriam;Frankie;Nick;Marilyn"
字符串值用分号分隔。
下面是我的sProc
。
问题出在输出上。
在此字符串上拆分的名字是 Elizabeth
而不是 John
。
它在哪里 John
?
所有其他名称都出现在 sProc
的输出中,只有 John
缺失...
我做错了什么?
BEGIN
DECLARE tNameSeries LONGTEXT;
DECLARE t_tNameSeries LONGTEXT;
SET tNameSeries = "John;Elizabeth;Mark;Zagor;Annie;Lucy;Peter;Robin;Wilson;Tom;Bettie;Myriam;Frankie;Nick;Marilyn";
WHILE LOCATE(";",tNameSeries) > 0 DO
SET tNameSeries = REPLACE (tNameSeries, (SELECT LEFT(tNameSeries,LOCATE(";",tNameSeries))),'');
SET t_tNameSeries = SUBSTRING_INDEX(tNameSeries,";",1);
SELECT t_tNameSeries;
END WHILE;
END
更新
使用这个编辑 sProc
输出只有 John
BEGIN
DECLARE tNameSeries LONGTEXT;
DECLARE t_tNameSeries LONGTEXT;
SET tNameSeries = "John;Elizabeth;Mark;Zagor;Annie;Lucy;Peter;Robin;Wilson;Tom;Bettie;Myriam;Frankie;Nick;Marilyn";
WHILE LOCATE(";",tNameSeries) > 0 DO
SET t_tNameSeries = SUBSTRING_INDEX(tNameSeries,";",1);
SET tNameSeries = REPLACE (t_tNameSeries, (SELECT LEFT(t_tNameSeries,LOCATE(";",t_tNameSeries))),'');
SELECT tNameSeries;
END WHILE;
END
SELECT *
FROM JSON_TABLE(
CONCAT(
'["',
REPLACE(
"John;Elizabeth;Mark;Zagor;Annie;Lucy;Peter;Robin;Wilson;Tom;Bettie;Myriam;Frankie;Nick;Marilyn",
';',
'","'
),
'"]'
),
'$[*]' COLUMNS (
id FOR ORDINALITY,
name VARCHAR(255) PATH '$'
)
) jsontable;
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=546907fc5c00b7173fa73327fdd97638
如果需要,将其插入 SP。
这也有效:
set @names = 'John;Elizabeth;Mark;Zagor;Annie;Lucy;Peter;Robin;Wilson;Tom;Bettie;Myriam;Frankie;Nick;Marilyn';
select
substring_index(substring_index(@names,';',R),';',-1) W
from (select row_number() over () as R
from information_schema.tables) x
where x.R<=1+length(@names)-length(replace(@names,';',''));
参见:https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=5cc442be9da54d8cbcdbabc58ee37b65
我需要在 MySQL
8 版本
stored Procedure
拆分此字符串
"John;Elizabeth;Mark;Zagor;Annie;Lucy;Peter;Robin;Wilson;Tom;Bettie;Myriam;Frankie;Nick;Marilyn"
字符串值用分号分隔。
下面是我的sProc
。
问题出在输出上。
在此字符串上拆分的名字是 Elizabeth
而不是 John
。
它在哪里 John
?
所有其他名称都出现在 sProc
的输出中,只有 John
缺失...
我做错了什么?
BEGIN
DECLARE tNameSeries LONGTEXT;
DECLARE t_tNameSeries LONGTEXT;
SET tNameSeries = "John;Elizabeth;Mark;Zagor;Annie;Lucy;Peter;Robin;Wilson;Tom;Bettie;Myriam;Frankie;Nick;Marilyn";
WHILE LOCATE(";",tNameSeries) > 0 DO
SET tNameSeries = REPLACE (tNameSeries, (SELECT LEFT(tNameSeries,LOCATE(";",tNameSeries))),'');
SET t_tNameSeries = SUBSTRING_INDEX(tNameSeries,";",1);
SELECT t_tNameSeries;
END WHILE;
END
更新
使用这个编辑 sProc
输出只有 John
BEGIN
DECLARE tNameSeries LONGTEXT;
DECLARE t_tNameSeries LONGTEXT;
SET tNameSeries = "John;Elizabeth;Mark;Zagor;Annie;Lucy;Peter;Robin;Wilson;Tom;Bettie;Myriam;Frankie;Nick;Marilyn";
WHILE LOCATE(";",tNameSeries) > 0 DO
SET t_tNameSeries = SUBSTRING_INDEX(tNameSeries,";",1);
SET tNameSeries = REPLACE (t_tNameSeries, (SELECT LEFT(t_tNameSeries,LOCATE(";",t_tNameSeries))),'');
SELECT tNameSeries;
END WHILE;
END
SELECT *
FROM JSON_TABLE(
CONCAT(
'["',
REPLACE(
"John;Elizabeth;Mark;Zagor;Annie;Lucy;Peter;Robin;Wilson;Tom;Bettie;Myriam;Frankie;Nick;Marilyn",
';',
'","'
),
'"]'
),
'$[*]' COLUMNS (
id FOR ORDINALITY,
name VARCHAR(255) PATH '$'
)
) jsontable;
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=546907fc5c00b7173fa73327fdd97638
如果需要,将其插入 SP。
这也有效:
set @names = 'John;Elizabeth;Mark;Zagor;Annie;Lucy;Peter;Robin;Wilson;Tom;Bettie;Myriam;Frankie;Nick;Marilyn';
select
substring_index(substring_index(@names,';',R),';',-1) W
from (select row_number() over () as R
from information_schema.tables) x
where x.R<=1+length(@names)-length(replace(@names,';',''));
参见:https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=5cc442be9da54d8cbcdbabc58ee37b65