-- MySQL字符串函数【返回子串位置find_in_set(s1,s2)】
/** FIND_IN_SET(s1,s2)函数返回在字符串s2中与s1匹配的字符串的位置.
字符串s2中包含了若干个用逗号隔开的字符串
*/
-- 字符串'like'在字符串'i,like,bei,jing'的第二个位置上
select find_in_set('like','i,like,bei,jing');
-- 【例子】:
-- 1).建临时表
create table test(
id bigint(20),
category varchar(255)
insert into test(id,category) values(1,'350,351,356,405');
insert into test(id,category) values(2,'350,351,405');
insert into test(id,category) values(3,'350,351');
select * from test ;
-- 作为where查询条件, 2个及以上的数值限制
select * from test where find_in_set('351',category) and find_in_set('405',category) ;
/** FIND_IN_SET(s1,s2)函数返回在字符串s2中与s1匹配的字符串的位置.
字符串s2中包含了若干个用逗号隔开的字符串
*/
-- 字符串'like'在字符串'i,like,bei,jing'的第二个位置上
select find_in_set('like','i,like,bei,jing');
-- 【例子】:
-- 1).建临时表
create table test(
id bigint(20),
category varchar(255)
);
-- 2).插入测试数据insert into test(id,category) values(1,'350,351,356,405');
insert into test(id,category) values(2,'350,351,405');
insert into test(id,category) values(3,'350,351');
select * from test ;
id | category |
1 | 350,351,356,405 |
2 | 350,351,405 |
3 | 350,351 |
-- 3).作为where查询条件,用来限制表中某个字符串字段,如产品分类字段中存放多个分类,逗号,来区别;字段中存放多个产品id等情况
select * from test where find_in_set('356',category);id | category |
1 | 350,351,356,405 |
-- 作为where查询条件, 2个及以上的数值限制
select * from test where find_in_set('351',category) and find_in_set('405',category) ;
id | category |
1 | 350,351,356,405 |
2 | 350,351,405 |