Home >Database >Mysql Tutorial >mysql函数split的学习_MySQL

mysql函数split的学习_MySQL

WBOY
WBOYOriginal
2016-06-01 13:40:411023browse

bitsCN.com
mysql函数split的学习 mysql 5.* 的版本现在没有split 函数,但有些地方会用,在这里就简单记录一下:先设置:SET GLOBAL log_bin_trust_function_creators = 1;  1. 函数func_splitStringTotal:将字符串按指定方式分割,并计算单元总数     Java代码  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:将字符串按指定方式分割,获取指定位置的数 Java代码  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 里面 Java代码  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

Statement:
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