DELIMITER $$ CREATE DEFINER = `root`@`%` FUNCTION `str_for_substr`(`num` int, `str` varchar(5000)) RETURNS varchar(100) BEGIN SET @str_for_substr = SUBSTRING( SUBSTRING_INDEX(str, ',', num + 1), CASE num WHEN 0 THEN CHAR_LENGTH( SUBSTRING_INDEX(str, ',', num) ) + 1 ELSE CHAR_LENGTH( SUBSTRING_INDEX(str, ',', num) ) + 2 END, CASE num WHEN 0 THEN CHAR_LENGTH( SUBSTRING_INDEX(str, ',', num + 1) ) - CHAR_LENGTH( SUBSTRING_INDEX(str, ',', num) ) ELSE CHAR_LENGTH( SUBSTRING_INDEX(str, ',', num + 1) ) - CHAR_LENGTH( SUBSTRING_INDEX(str, ',', num) ) - 1 END ); RETURN @str_for_substr; END$$ DELIMITER ;
测试:
mysql> SELECT str_for_substr(3,'one,two,three,four,five,six,seven') as 'value';
+-------+
| value |
+-------+
| four |
+-------+
1 row in set
搜索
复制
<iframe></iframe>