要从逗号分隔的列中搜索,请使用FIND_IN_SET()方法。让我们首先创建一个表-
mysql> create table DemoTable -> ( -> Value varchar(20) -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values('41,14,94'); mysql> insert into DemoTable values('64,84,94'); mysql> insert into DemoTable values('44,74,103,104'); mysql> insert into DemoTable values('904,1004,1444,1544'); mysql> insert into DemoTable values('4,144,174');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+--------------------+ | Value | +--------------------+ | 41,14,94 | | 64,84,94 | | 44,74,103,104 | | 904,1004,1444,1544 | | 4,144,174 | +--------------------+ 5 rows in set (0.00 sec)
这是从逗号分隔的MySQL列中搜索的查询-
mysql> select *from DemoTable where find_in_set('4',Value);
这将产生以下输出-
+-----------+ | Value | +-----------+ | 4,144,174 | +-----------+ 1 row in set (0.00 sec)