从MySQL中以逗号分隔值的列中查找特定记录
mysqlmysqli database
为此,您可以使用 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)