FIND_IN_SET(Var, Field)

1
2
3
4
5
6
7
8
9
10
CREATE TABLE `user_org` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(30) DEFAULT NULL,
`organizations` varchar(60) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB

insert into user_org (name, organizations) values('Betty', '1001,1002,1003');
insert into user_org (name, organizations) values('William', '1001,1002');
insert into user_org (name, organizations) values('Thomas', '1002,1003');

Mysql

User find_in_set function directly.

1
select * from user_org where find_in_set('1003', organizations);

Oracle

  1. Use LIKE key word:
    1
    select * from user_org where organizations like '%1003%';

this may mis match, if element length are not fixed.

  1. Create a function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
CREATE OR REPLACE
FUNCTION FIND_IN_SET(piv_str1 varchar2, piv_str2 varchar2, p_sep varchar2 := ',')
RETURN NUMBER IS
l_idx number:=0; -- 用于计算piv_str2中分隔符的位置
str varchar2(500); -- 根据分隔符截取的子字符串
piv_str varchar2(500) := piv_str2; -- 将piv_str2赋值给piv_str
res number:=0; -- 返回结果
res_place number:=0; -- 原字符串在目标字符串中的位置
BEGIN
-- 如果piv_str中没有分割符,直接判断piv_str1和piv_str是否相等,相等 res_place=1
IF instr(piv_str, p_sep, 1) = 0THEN
IF piv_str = piv_str1 THEN
res_place:=1;
END IF;
ELSE
-- 循环按分隔符截取piv_str
LOOP
l_idx := instr(piv_str,p_sep);
--
res_place := res_place + 1;
-- 当piv_str中还有分隔符时
IF l_idx > 0THEN
-- 截取第一个分隔符前的字段str
str:= substr(piv_str,1,l_idx-1);
-- 判断 str 和piv_str1 是否相等,相等则结束循环判断
IF str = piv_str1 THEN
res:= res_place;
EXIT;
END IF;
piv_str := substr(piv_str,l_idx+length(p_sep));
ELSE
-- 当截取后的piv_str 中不存在分割符时,判断piv_str和piv_str1是否相等,相等 res=res_path
IF piv_str = piv_str1 THEN
res:= res_place;
END IF;
-- no matter what jump out
EXIT;
END IF;
END LOOP;
-- stoped
END IF;
res := res_place;
-- return
RETURN res;
END FIND_IN_SET;