关于mysql--- 函数
一. 创建一个自定义 函数 细节:
1. 源代码:
drop function if exists hello; -- 如果有该函数,则删除
create function hello(a int) returns varchar (8) -- 创建叫做hello的函数, 传入a , 返回varchar类型
begin
declare x varchar(255); -- 声明一个变量x
declare y int default 0;
SELECT `group` INTO x FROM t_user WHERE id=a; -- 查询sql, 返回值塞入x
repeat -- 循环开始 // 循环
set x = substring_index(substring_index(x,',',-2),',',1); -- 赋值 要用set
SET y=y+1;
until x='2' end repeat; -- 循环结束 // 记住是= , 不是==
return x;
end;
2. 执行
SELECT hello(12);
二、 一些易疏忽函数与功能
1. substring_index(x, ' ,' , -2)
分割x 用 ,号 并返回倒数第二个 字符串
例如: substring_index('123,3df,5w,6c', ' ,' , -2)
此时返回5w
2. mysql.help_topic
select a.id, a.name,
substring_index(substring_index(a.group, ',', b.help_topic_id + 1), ',', -1) group1
from t_user a
JOIN mysql.help_topic b
ON b.help_topic_id < (length(a.group) - length(REPLACE(a.group, ',', '')) + 1)
使用以上语句后,查询结果:
这样可以帮你把,号分割了