Home > Database > Mysql Tutorial > mysql函数split功能实现_MySQL

mysql函数split功能实现_MySQL

WBOY
Release: 2016-06-01 13:23:01
Original
1152 people have browsed it

bitsCN.com 先设置: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 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;
bitsCN.com

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template