MySQL查询在一个字段内的逗号分隔值之间进行搜索?

为此使用FIND_IN_SET。让我们首先创建一个表-

mysql> create table DemoTable
-> (
-> ListOfValues text
-> );

使用插入命令在表中插入一些记录-

mysql> insert into DemoTable values('10|20|30|40|50|60|100');

使用select语句显示表中的所有记录-

mysql> select *from DemoTable;

输出结果

这将产生以下输出-

+-----------------------+
| ListOfValues          |
+-----------------------+
| 10|20|30|40|50|60|100 |
+-----------------------+
1 row in set (0.00 sec)

以下是在一个字段内以逗号分隔的值之间进行搜索的查询-

mysql> select FIND_IN_SET(100, replace(ListOfValues,'|',',')) from DemoTable;

输出结果

这将产生以下输出-

+-------------------------------------------------+
| FIND_IN_SET(100, replace(ListOfValues,'|',',')) |
+-------------------------------------------------+
| 7                                               |
+-------------------------------------------------+
1 row in set (0.04 sec)