从一列中查找特定记录,并在MySQL中用逗号分隔值
为此,您可以使用FIND_IN_SET()。让我们首先创建一个表-
mysql> create table DemoTable -> ( -> ListOfValue varchar(20) -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values('78,89,65'); mysql> insert into DemoTable values('88,96,97'); mysql> insert into DemoTable values('95,96,99,100'); mysql> insert into DemoTable values('78,45,67,98');
使用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)