广告
返回顶部
首页 > 资讯 > 数据库 >mysql split函数用逗号分隔的实现
  • 279
分享到

mysql split函数用逗号分隔的实现

mysqlsplit逗号分隔mysql逗号分隔 2022-05-14 22:05:12 279人浏览 独家记忆
摘要

定义存储过程,用于分隔字符串 DELIMITER $$ USE `mess`$$ DROP PROCEDURE IF EXISTS `splitString`$$ CREATE DEFINER=`root

定义存储过程,用于分隔字符串


DELIMITER $$
USE `mess`$$
DROP PROCEDURE IF EXISTS `splitString`$$
CREATE DEFINER=`root`@`%` 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_get_splitStringTotal(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_splitString(f_string,f_delimiter,i));  
  END WHILE;  
END$$
DELIMITER ;

实现func_get_splitStringTotal函数:该函数用于计算分隔之后的长度,这里需要了解的函数:


REPLACE(str,from_str,to_str)

Returns the string str with all occurrences of the string from_str replaced by the string to_str. REPLACE() perfORMs a case-sensitive match when searching for from_str.
例如:
Mysql> SELECT REPLACE('www.mysql.com', 'w', 'Ww');
    -> 'WwWwWw.mysql.com'

具体实现:


DELIMITER $$

USE `mess`$$

DROP FUNCTioN IF EXISTS `func_get_splitStringTotal`$$

CREATE DEFINER=`root`@`%` 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 ;

实现func_splitString函数:用于获取分隔之后每次循环的值,这里需要了解的函数:


(1)REVERSE(str)

Returns the string str with the order of the characters reversed.
例如:mysql> SELECT REVERSE('abc');
    -> 'cba'

(2)
SUBSTRING_INDEX(str,delim,count)


Returns the substring from string str before count occurrences of the delimiter delim. If count is positive, everything to the left of the final delimiter (counting from the left) is returned. If count is negative, everything to the right of the final delimiter (counting from the right) is returned. SUBSTRING_INDEX() performs a case-sensitive match when searching for delim.

例如:
mysql> SELECT SUBSTRING_INDEX('www.mysql.com', '.', 2);
    -> 'www.mysql'
mysql> SELECT SUBSTRING_INDEX('www.mysql.com', '.', -2);
    -> 'mysql.com'

具体实现:


DELIMITER $$

USE `mess`$$

DROP FUNCTION IF EXISTS `func_splitString`$$

CREATE DEFINER=`root`@`%` 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$$

DELIMITER ;

使用:

(1)调用存储过程:

CALL splitString('1,3,5,7,9',',');

(2):查看临时表

SELECT val_ FROM tmp_split AS t1;

 结果:


到此这篇关于mysql split函数用逗号分隔的实现的文章就介绍到这了,更多相关mysql split逗号分隔内容请搜索自学编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持自学编程网!

您可能感兴趣的文档:

--结束END--

本文标题: mysql split函数用逗号分隔的实现

本文链接: https://www.lsjlt.com/news/9916.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作