[英]Split a mysql field into multiple rows
I have a table which has a field called "Codes", like this;我有一个表,其中有一个名为“代码”的字段,如下所示;
The result would be something like this(This is from another db, need the query:!):结果将是这样的(这是来自另一个数据库,需要查询:!):
Another example:另一个例子:
Note that the pro_ parent_code field is the first @@ of the Code column.请注意, pro_parent_code字段是Code列的第一个 @@。
My query:我的查询:
SELECT
SUBSTRING_INDEX(SUBSTRING_INDEX(Codigos, '@@', 1), '@@', 1) AS pro_codigo,
SUBSTRING_INDEX(SUBSTRING_INDEX(Codigos, '@@', 1), ' ', -1) AS pro_codigo_padre
FROM table
the result:结果:
Create a function to Split the values创建 function 以拆分值
Create FUNCTION [dbo].[Func_Split]
(
@String varchar(max),
@Delimiter char
)
RETURNS @Temp TABLE (Srno int identity, Items varchar(max))
AS
begin
Declare @Index int=1, @Item varchar(max)
if(Len(@String)<=0 or isnull(@String, '')='')
begin
return
end
else
begin
while @Index>0
begin
set @Index = CHARINDEX(@Delimiter, @String)
if(@Index>0)
begin
set @Item = Left(@String, @Index-1)
end
else
begin
set @Item=@String
end
if(Len(@Item)>0)
begin
insert into @Temp
(Items)
values
(@Item)
end
set @String = RIGHT(@String, Len(@String)-@Index)
IF len(@String) = 0 BREAK
end
end
Return
end
Modify your query accordingly to use it in your query相应地修改您的查询以在您的查询中使用它
Eg例如
select Items from dbo.Func_Split('XYZ@@123@@456','@@')
Here are two methods.这里有两种方法。
The first uses recursion.第一个使用递归。
The second uses a Tally table/query with numbers.第二个使用带有数字的 Tally 表/查询。
create table Codes ( id int auto_increment primary key, code varchar(100) ) insert into Codes (code) values ('ADV000001@@TB1234567@@TB8901234@@TB5678901@@')
with recursive CTE as ( select 1 as n, SUBSTRING_INDEX(SUBSTRING_INDEX(code, '@@', 1), '@@', -1) as child, SUBSTRING_INDEX(SUBSTRING_INDEX(code, '@@', 1), '@@', -1) as parent from Codes union all select n+1, SUBSTRING_INDEX(SUBSTRING_INDEX(code, '@@', n+1), '@@', -1) as child, SUBSTRING_INDEX(SUBSTRING_INDEX(code, '@@', 1), '@@', -1) as parent from Codes join CTE on n < (length(code)-length(replace(code,'@@',','))) ) select child as code, nullif(parent, child) as code_parent from CTE
code |代码 | code_parent:-------- |:---------- ADV000001 |
code_parent:-------- |:---------- ADV000001 | null TB1234567 |
null TB1234567 | ADV000001 TB8901234 |
ADV000001 TB8901234 | ADV000001 TB5678901 |
ADV000001 TB5678901 | ADV000001
ADV000001
select SUBSTRING_INDEX(SUBSTRING_INDEX(code, '@@', n), '@@', -1) as child,SUBSTRING_INDEX(SUBSTRING_INDEX(code, '@@', 1), '@@', -1) as parent from Codes join (select 0 as n union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9 ) nums on n between 1 and (length(code)-length(replace(code,'@@',',')))
child |孩子| parent:-------- |:-------- ADV000001 |
父:-------- |:-------- ADV000001 | ADV000001 TB1234567 |
ADV000001 TB1234567 | ADV000001 TB8901234 |
ADV000001 TB8901234 | ADV000001 TB5678901 |
ADV000001 TB5678901 | ADV000001
ADV000001
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.