日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

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

發布時間:2023/12/15 数据库 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql逗号分隔函数_mysql split 函数(用逗号分隔)的实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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;

結果:

總結

以上是生活随笔為你收集整理的mysql逗号分隔函数_mysql split 函数(用逗号分隔)的实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。