要搜索以逗号分隔的列表,请使用MySQL find_in_set()。这里不需要为此使用正则表达式。语法如下-
select *from yourTableName where find_in_set(anyValue,yourColumnName);
让我们创建一个表-
mysql> create table demo17 −> ( −> id int not null auto_increment primary key, −> first_name varchar(50), −> value text −> );
借助insert命令将一些记录插入表中-
mysql> insert into demo17(first_name,value) values('John','50'); mysql> insert into demo17(first_name,value) values('David',''); mysql> insert into demo17(first_name,value) values('Adam','50,89'); mysql> insert into demo17(first_name,value) values('Adam','49,89,43');
使用select语句显示表中的记录-
mysql> select *from demo17;
这将产生以下输出-
+----+------------+----------+ | id | first_name | value | +----+------------+----------+ | 1 | John | 50 | | 2 | David | | | 3 | Adam | 50,89 | | 4 | Adam | 49,89,43 | +----+------------+----------+ 4 rows in set (0.00 sec)
以下是查找字符串和下一个字符的查询-
mysql> select *from demo17 where find_in_set(50,value);
这将产生以下输出-
+----+------------+-------+ | id | first_name | value | +----+------------+-------+ | 1 | John | 50 | | 3 | Adam | 50,89 | +----+------------+-------+ 2 rows in set (0.00 sec)