是否可以从由逗号分隔的 MySQL 列进行搜索?
要从逗号分隔的列中进行搜索,请使用 FIND_IN_SET() 方法。让我们首先创建一个数据表 -
mysql> create table DemoTable -> ( -> Value varchar(20) -> ); Query OK, 0 rows affected (0.47 sec)
使用插入命令在表中插入一些记录 -
mysql> insert into DemoTable values('41,14,94'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('64,84,94'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('44,74,103,104'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('904,1004,1444,1544'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('4,144,174'); Query OK, 1 row affected (0.15 sec)
使用选择语句从表中显示所有记录 -
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)
广告