在mySql中 拆分字串

2021-06-10 12:06:18 字數 3463 閱讀 1653

# drop temporary table if exists test_tim;  刪錶用

create temporary table test_tim (

id int,

a1 varchar(128),

a2 decimal(16,2),

a21 decimal(16,2),

a3 varchar(1024),

a4 datetime,

a5 int,

a6 int

);#drop temporary table if exists test;  刪錶用

create temporary table test (

id int,

a1 varchar(128),

a2 decimal(16,2),

a21 decimal(16,2),

a3 varchar(1024),

a4 datetime,

a5 int,

a6 int

);#插入樣本

insert into test values(136,'test',1234567.8,987654.2,'com.a.b,1,6,error,ioeoffilea,b','2012/10/22 12:00',0,4);

insert into test values(137,'test2',1234567.9,987654.3,'com.a.b,1,6,error,ioeoffileb,c','2012/10/22 13:00',1,5);

#校驗 select * from test;

136test

1234567.80

987654.20

com.a.b,1,6,error,ioeoffilea,b

2012-10-22 12:00:0004

137test2

1234567.90

987654.30

com.a.b,1,6,error,ioeoffileb,c

2012-10-22 13:00:0015

#處理邏輯

delimiter $$;

create procedure lhy()

reads sql data

begin

declare did int;

declare da1 varchar(128);

declare da2 decimal(16,2);

declare da21 decimal(16,2);

declare da3 varchar(1024);

declare da4 datetime;

declare da5 int;

declare da6 int;    

declare stop int default 0;

declare _cs cursor for     select id, a1, a2, a21, a3,a4,a5,a6 from test;

declare continue handler for sqlstate '02000' set stop=1;

open _cs;

fetch _cs into did, da1, da2, da21, da3,da4,da5,da6;

while stop<>1 do

begin

declare i int;  

declare s int;

set i=1 ;

set s=1  ;

while(i>0) do     

set i=locate(',',da3,s)  ;    

if(i>0) then             

insert test_tim values(did, da1, da2, da21, substring(da3,s,i-s),da4,da5,da6 ) ;       

end if;         

if(i<=0) then       

insert test_tim values(did, da1, da2, da21, substring(da3,s,length(da3)-s+1),da4,da5,da6) ;

end if;

set s = i + 1   ;

end  while;

end;

fetch _cs into did, da1, da2, da21, da3,da4,da5,da6;

end while;

close _cs;

end$$;

#drop procedure lhy;刪除procedure 用

#call lhy; 呼叫lhy用

#校驗結果用--先create ,再call,再校驗結果

#select * from test_tim;

136test

1234567.80

987654.20

com.a.b

2012-10-22 12:00:0004

136test

1234567.80

987654.20

12012-10-22 12:00:0004

136test

1234567.80

987654.20

62012-10-22 12:00:0004

136test

1234567.80

987654.20

error

2012-10-22 12:00:0004

136test

1234567.80

987654.20

ioeoffilea

2012-10-22 12:00:0004

136test

1234567.80

987654.20

b2012-10-22 12:00:0004

137test2

1234567.90

987654.30

com.a.b

2012-10-22 13:00:0015

137test2

1234567.90

987654.30

12012-10-22 13:00:0015

137test2

1234567.90

987654.30

62012-10-22 13:00:0015

137test2

1234567.90

987654.30

error

2012-10-22 13:00:0015

137test2

1234567.90

987654.30

ioeoffileb

2012-10-22 13:00:0015

137test2

1234567.90

987654.30

c2012-10-22 13:00:0015

mysql拆分字串

函式 1 從左開始擷取字串 left str,length 說明 left 被擷取字段,擷取長度 例 select left make date,4 as year from t sale billing where make date 2017 06 24 2 從右開始擷取字串 right str...

mysql拆分字串函式

業務需求 拆分字串,然後將數字轉換成中文描述,返回成以,分割的中文描述 修改結束符,防止在mysql命令列中預設分號直接執行 delimiter 建立乙個計算拆分後字串的個數函式 drop function if exists calc length create function calc len...

拆分字串

拆分乙個字串,獲取每一組的key與value。如字串 qq adf f qewr98 eer d9adf t ad34 f qewrqr u adf43 gggg 2344 按照物件導向理念來解決,建立乙個物件 這個是對物件物件,有key和value兩個特性。我們需要把拆分好的資料臨時儲存起來,現在...