mysql split函数用逗号分隔的实现
作者:xinglei200909931 时间:2024-01-19 12:46:40
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 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 ;
2:实现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 ;
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 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;
结果:
来源:https://blog.csdn.net/xinglei200909931/article/details/84830809
标签:mysql,split,逗号分隔
0
投稿
猜你喜欢
python中的print()输出
2023-06-27 18:07:03
Python get获取页面cookie代码实例
2021-03-29 17:26:49
PHP下常用正则表达式整理
2023-11-18 03:04:48
Python3操作SQL Server数据库(实例讲解)
2024-01-24 04:13:21
PHP之mysql位运算案例讲解
2023-06-13 06:16:19
在python里创建一个任务(Task)实例
2023-09-12 23:24:16
vue项目使用高德地图的定位及关键字搜索功能的实例代码(踩坑经验)
2024-05-09 15:21:01
Pytest+request+Allure实现接口自动化框架
2023-08-12 17:29:33
微信小程序学习笔记之本地数据缓存功能详解
2024-04-22 12:42:49
MySQL8忘记密码的快速解决方法
2024-01-19 14:58:26
Java 数据库连接池c3p0 介绍
2024-01-22 02:44:24
Django Auth用户认证组件实现代码
2022-05-02 18:06:01
python函数超时自动退出的实操方法
2021-09-11 07:07:45
Go素数筛选分析详解
2023-07-22 11:50:02
Mysql的列修改成行并显示数据的简单实现
2024-01-24 01:39:54
python3简单实现微信爬虫
2022-10-22 20:55:12
python中验证码连通域分割的方法详解
2022-09-30 11:04:00
pytorch中可视化之hook钩子
2021-07-30 04:26:23
Java+MySql图片数据保存与读取的具体实例
2024-01-22 01:07:31
如何创建SQL Server 2000故障转移群集
2009-02-13 17:18:00