要从逗号分隔的值中获取记录,请使用MySQL FIND_IN_SET()。让我们首先创建一个表-
mysql> create table DemoTable1548 -> ( -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> StudentName varchar(20), -> ArrayListOfMarks varchar(100) -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1548(StudentName,ArrayListOfMarks) values('Chris','56,78,90,87'); mysql> insert into DemoTable1548(StudentName,ArrayListOfMarks) values('Bob','90,78,65'); mysql> insert into DemoTable1548(StudentName,ArrayListOfMarks) values('David','91,34,56,78,87');
使用select语句显示表中的所有记录-
mysql> select * from DemoTable1548;
这将产生以下输出-
+-----------+-------------+------------------+ | StudentId | StudentName | ArrayListOfMarks | +-----------+-------------+------------------+ | 1 | Chris | 56,78,90,87 | | 2 | Bob | 90,78,65 | | 3 | David | 91,34,56,78,87 | +-----------+-------------+------------------+ 3 rows in set (0.00 sec)
这是从逗号分隔值中提取匹配的特定记录的查询-
mysql> select * from DemoTable1548 where find_in_set('87',ArrayListOfMarks);
这将产生以下输出-
+-----------+-------------+------------------+ | StudentId | StudentName | ArrayListOfMarks | +-----------+-------------+------------------+ | 1 | Chris | 56,78,90,87 | | 3 | David | 91,34,56,78,87 | +-----------+-------------+------------------+ 2 rows in set (0.00 sec)