我如何在 MySQL 中将逗号分隔列表与一个值进行匹配?
我们首先创建一个表 -
mysql> create table DemoTable ( `Values` varchar(50) ); Query OK, 0 rows affected (1.15 sec)
使用插入命令在表中插入一些记录 -
mysql> insert into DemoTable values('23,45,78,56'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('384,476,7456'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('68,8,88,89'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('78,80,84'); Query OK, 1 row affected (0.19 sec)
使用 select 语句从表中显示所有记录 -
mysql> select *from DemoTable;
这将产生以下输出 -
+--------------+ | Values | +--------------+ | 23,45,78,56 | | 384,476,7456 | | 68,8,88,89 | | 78,80,84 | +--------------+ 4 rows in set (0.00 sec)
以下是匹配逗号分隔列表与一个值的查询 -
mysql> select *from DemoTable where find_in_set('8',`Values`)!=0;
这将产生以下输出 -
+------------+ | Values | +------------+ | 68,8,88,89 | +------------+ 1 row in set (0.00 sec)
广告