MySQL查询以获取与数组匹配的特定记录(以逗号分隔的值)
要从逗号分隔的值中获取记录,请使用MySQLFIND_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)
热门推荐
1 潜能开发训练狗狗
2 狗狗多会训练好
3 扣篮训练狗狗
5 狗狗不训练
6 少儿柔术训练狗狗
8 训练好狗狗
9 训练狗狗怎么训
10 狗狗训练猫咪
11 狗狗训练地点
12 两个多月的狗狗怎么训练
13 怎么训练狗狗去厕所拉屎
14 狗狗怎样训练大小便
15 训练狗狗母
16 怎么训练狗狗坐下和握手
17 近视训练狗狗
18 狗狗倒的训练