今天用navicat 不能创建函数,查询了
MySQL函数不能创建,是未开启功能:
- mysql> show variables like '%func%';
- +---------------------------------+-------+
- | Variable_name | Value |
- +---------------------------------+-------+
- | log_bin_trust_function_creators | OFF |
- +---------------------------------+-------+
- 1 row in set (0.00 sec)
- mysql> set global log_bin_trust_function_creators=1;
- Query OK, 0 rows affected (0.00 sec)
- mysql> show variables like '%func%';
- +---------------------------------+-------+
- | Variable_name | Value |
- +---------------------------------+-------+
- | log_bin_trust_function_creators | ON |
- +---------------------------------+-------+
- 1 row in set (0.00 sec)mysql>
==================
这里写了2个函数,功能是去除用逗号分隔的字符串中重复的字符
CREATE DEFINER=`root`@`localhost` FUNCTION `str_str_unique`(str text,str1 text) RETURNS text CHARSET utf8
BEGIN
#str 要处理的字符 str1 分隔符
#功能:处理字符串中重复的字符
declare stri TEXT;
declare i int DEFAULT 1;
declare len int DEFAULT 0;
declare tmp TEXT default '' ;
declare tmp_str varchar(255) default '' ;
declare site int DEFAULT 0;
set len = str_str_count(str,str1);
repeat
set tmp_str=SUBSTRING_INDEX(SUBSTRING_INDEX(str,str1,i),str1,-1);
set site = FIND_IN_SET(tmp_str,tmp);
if(site=0 and tmp_str is not null and tmp_str !='') then
if(length(tmp)>0)then
set tmp = concat(tmp,str1,tmp_str);
else
set tmp = concat(tmp,tmp_str);
end if;
end if;
set i = i + 1;
until i >len+2 end repeat;
set stri = tmp;
return stri;
END
CREATE DEFINER=`root`@`%` FUNCTION `str_str_count`(str varchar(255),stri varchar(20) ) RETURNS int(12)
BEGIN
# 测量字符串中分隔符的个数
DECLARE total int(20);
SELECT LENGTH(str) - LENGTH(REPLACE(str, stri, '')) into total;
RETURN total;
END