MySql分割字串 儲存過程

2022-02-10 14:55:19 字數 924 閱讀 4313

mysql沒有表變數,通過函式無法返回表。

參考**:

delimiter $$

use `資料庫`$$

drop procedure if exists `split`$$

create definer=`sa_scmone`@`%` procedure `split`(in _string varchar(300))

begin

# 求分割符號','的位置

declare _index int;

#使用臨時表儲存分割後的結果

drop temporary table if exists tmp_strs;

create temporary table tmp_strs(

str int(10) unsigned);

set _index = locate(',',_string);

while _index > 0

doinsert into tmp_strs values(left(_string,_index-1));#將子字串存入臨時表

set _string =substr(_string from _index+1);

set _index = locate(',',_string);

end while;

if length(_string) >= 0 then

insert into tmp_strs values(_string);

end if;

end$$

delimiter ;

測試**:

call split('1,2,3,4,5'); 

select * from tmp_strs

結果:str

--------12345

mysql 字串分割儲存過程

drop procedure if exists splitstr create procedure splitstr in str varchar 255 out substr varchar 2000 out idstr varchar 100 begin declare index var v...

Mysql儲存過程中字串分割

今在專案中碰到了要把字串分割,記錄下來,以後可能還用的到 首先想上我的儲存過程 delimiter use bplate drop procedure if exists lp plate insertplateinfo create definer root localhost procedure...

mysql分割字串 mysql分割字串

專案有通過一批id去過濾結果的需求,因為這個id是從其他平台拉下來的excel,為了避免加引號逗號的麻煩,在mysql儲存過程裡面拼接。在此做個記錄。很多地方用得上。1.通過某個字元,分割字串的函式。輸入分別為f string 待分割字串 f delimiter 分割字元 f order 取的字串的...