通过 MySQL 从逗号分隔值列中查找特定记录
对此,你可以使用 FIND_IN_SET()。让我们先创建一个表 -
mysql> create table DemoTable -> ( -> ListOfValue varchar(20) -> ); Query OK, 0 rows affected (0.52 sec)
使用 insert 命令插入一些记录到表中 -
mysql> insert into DemoTable values('78,89,65'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('88,96,97'); Query OK, 1 row affected (0.30 sec) mysql> insert into DemoTable values('95,96,99,100'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('78,45,67,98'); Query OK, 1 row affected (0.10 sec)
使用 select 语句显示表中的所有记录 -
mysql> select * from DemoTable;
这将会产生以下输出 -
+--------------+ | ListOfValue | +--------------+ | 78,89,65 | | 88,96,97 | | 95,96,99,100 | | 78,45,67,98 | +--------------+ 4 rows in set (0.00 sec)
以下是如何通过逗号分隔值从列中查找特定记录的查询 -
mysql> select * from DemoTable -> where find_in_set('89',ListOfValue) -> or -> find_in_set('99',ListOfValue);
这将会产生以下输出 -
+--------------+ | ListOfValue | +--------------+ | 78,89,65 | | 95,96,99,100 | +--------------+ 2 rows in set (0.00 sec)
广告