MySQL查询从逗号分隔的列表部分检索记录?
要从逗号分隔的列表部分检索记录,可以使用内置函数FIND_IN_SET()。
让我们首先创建一个表-
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Name varchar(20), Marks varchar(200) );
以下是使用insert命令在表中插入一些记录的查询-
mysql> insert into DemoTable(Name,Marks) values('Larry','98,34,56,89');
mysql> insert into DemoTable(Name,Marks) values('Chris','67,87,92,99');
mysql> insert into DemoTable(Name,Marks) values('Robert','33,45,69,92');以下是查询以使用select命令显示表中的记录-
mysql> select *from DemoTable;
这将产生以下输出-
+----+--------+-------------+ | Id | Name | Marks | +----+--------+-------------+ | 1 | Larry | 98,34,56,89 | | 2 | Chris | 67,87,92,99 | | 3 | Robert | 33,45,69,92 | +----+--------+-------------+ 3 rows in set (0.00 sec)
以下是从逗号分隔列表部分检索记录的查询。在这里,我们获得了一个分数为99的学生的记录-
mysql> select Id,Name from DemoTable where find_in_set('99',Marks) > 0;这将产生以下输出-
+----+-------+ | Id | Name | +----+-------+ | 2 | Chris | +----+-------+ 1 row in set (0.00 sec)
热门推荐
10 广西考试祝福语结婚简短
11 猪年祝福语简短小孩
12 元旦祝福语送长辈简短
13 恭喜二宝祝福语简短
14 祝福语暖心话简短
15 国庆中秋祝福语简短兄弟
16 朋友订婚的祝福语简短
17 送弟弟中秋祝福语简短
18 爱生日祝福语简短独特