) RETURNS varchar(255) CHARSET latin1
begin
RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(str, delim, pos),LENGTH(SUBSTRING_INDEX(str,delim, pos -1)) + 1),delim,'');
由于这个函数里没有任何SQL语句,会提示:
Error: This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_creators variable)
这里需要修改一个变量值:
set global log_bin_trust_function_creators = ON;
CREATE FUNCTION `str_split`( str VARCHAR(255), delim VARCHAR(12), pos INT) RETURNS varchar(255) CHARSET latin1begin RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(str, delim, pos),LENGTH(SUB
实现方式:
sELECT
SUB
STR
ING_INDEX(SUB
STR
ING_INDEX('7654,7698,7782,7788',',',help_topic_id+1),',',-1) AS num
mysql
.h..
SELECT
SUB
STR
ING_INDEX(SUB
STR
ING_INDEX('7654,7698,7782,7788',',',help_topic_id+1),',',-1) AS num
mysql
.help_topic
END$$
在上面的代码中,我们首先需要定义
分隔符
“delimiter”,然后使用循环来查找该
分隔符
在
字符串
中出现的位置,以确定
字符串
中有多少个子
字符串
。通过逐步移动起始位置startPos,我们可以按顺序查找每个子
字符串
,并使用变量arrayLength进行计数。
最后,我们可以使用RETURN语句将数组长度作为
函数
的返回值。在使用该
函数
时,只需将要分割的
字符串
和
分隔符
传递给该
函数
即可。
例:SELECT getArrayLength('Apple,Orange,Mango,Pineapple',',')将返回值4,因为该
字符串
有4个用逗号分隔的子
字符串
。