mysql函數split功效完成。本站提示廣大學習愛好者:(mysql函數split功效完成)文章只能為提供參考,不一定能成為您想要的結果。以下是mysql函數split功效完成正文
先設置:SET GLOBAL log_bin_trust_function_creators = 1;
1. 函數func_splitStringTotal:將字符串按指定方法朋分,並盤算單位總數
DELIMITER $$
CREATE FUNCTION `func_get_splitStringTotal`(
f_string varchar(10000),f_delimiter varchar(50)
) RETURNS int(11)
BEGIN
return 1+(length(f_string) - length(replace(f_string,f_delimiter,'')));
END$$
DELIMITER ;
2.函數func_splitString:將字符串按指定方法朋分,獲得指定地位的數
DELIMITER $$
DROP function IF EXISTS `func_splitString` $$
CREATE FUNCTION `func_splitString`
( f_string varchar(1000),f_delimiter varchar(5),f_order int)
RETURNS varchar(255) CHARSET utf8
BEGIN
declare result varchar(255) default '';
set result = reverse(substring_index(reverse(substring_index(f_string,f_delimiter,f_order)),f_delimiter,1));
return result;
END$$
SELECT func_splitString('1,2,3,4,5,6,7',',',1);
3.進程splitString 將字符串朋分,並放莅臨時表tmp_split 外面
DELIMITER $$
DROP PROCEDURE IF EXISTS `splitString` $$
CREATE PROCEDURE `splitString`
(IN f_string varchar(1000),IN f_delimiter varchar(5))
BEGIN
declare cnt int default 0;
declare i int default 0;
set cnt = func_split_TotalLength(f_string,f_delimiter);
DROP TABLE IF EXISTS `tmp_split`;
create temporary table `tmp_split` (`val_` varchar(128) not null) DEFAULT CHARSET=utf8;
while i < cnt
do
set i = i + 1;
insert into tmp_split(`val_`) values (func_split(f_string,f_delimiter,i));
end while;
END$$
call splitString('a,s,d,f,g,h,j',',');
SELECT * from tmp_split;