mysql split函数用逗号分隔的实现
网络编程 2021-07-05 14:37www.168986.cn编程入门
这篇文章主要介绍了mysql split函数用逗号分隔的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们狼蚁网站SEO优化随着长沙网络推广来一起学习学习吧
1:定义存储过程,用于分隔字符串
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 t INT DEFAULT 0; DECLARE i INT DEFAULT 0; SET t = 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 < t DO SET i = i + 1; INSERT INTO tmp_split(`val_`) VALUES (func_splitString(f_string,f_delimiter,i)); END WHILE; END$$ DELIMITER ;
2:实现func_get_splitStringTotal函数:该函数用于计算分隔之后的长度,这里需要了解的函数:
REPLACE(str,from_str,to_str) Returns the string str with all ourrences 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('.mysql.', 'w', 'Ww'); -> 'WwWwWw.mysql.'
具体实现:
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 ;
3:实现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 ourrences 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('.mysql.', '.', 2); -> '.mysql' mysql> SELECT SUBSTRING_INDEX('.mysql.', '.', -2); -> 'mysql.'
具体实现:
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逗号分隔内容请搜索狼蚁SEO以前的文章或继续浏览狼蚁网站SEO优化的相关文章希望大家以后多多支持狼蚁SEO!
编程语言
- 机械手焊接机器人编程 机械手焊接设备编程
- vb数据库编程实例,vb做数据库
- tiobe世界编程语言排行榜,全球编程语言排行
- 成都java编程培训 成都it编程培训中心
- 编程基础培训,编程基础知识培训
- flash游戏编程基础教程,flash游戏开发教程
- 不会英语能学编程吗 不会英语可以学程序员吗
- 爱心代码编程C语言 爱心代码编程c语言公式
- c语言编程学习入门 c语言编程快速入门
- 西门子plc编程培训 西门子plc编程培训班
- 哪里可以学编程 台州哪里可以学编程
- 少儿编程哪个好 少儿编程哪个品牌好
- 编程是什么意思,图形编程是什么意思
- c语言编程软件下载,c语言编程软件在哪下载
- java编程软件下载,java编程下载手机版
- ug编程培训学校,ug编程培训教程