获取MySQL IN子句中返回的记录集顺序?
对于返回的记录集顺序,您需要使用FIND_IN_SET()函数。
例如,让我们创建一个表。
mysql> create table returnRecordSetOrderDemo -> ( -> Id int, -> Name varchar(20) -> );
使用insert命令在表中插入一些记录。
查询如下。
mysql> insert into returnRecordSetOrderDemo values(100,'John'); mysql> insert into returnRecordSetOrderDemo values(130,'Carol'); mysql> insert into returnRecordSetOrderDemo values(103,'Bob'); mysql> insert into returnRecordSetOrderDemo values(134,'Sam'); mysql> insert into returnRecordSetOrderDemo values(102,'Larry'); mysql> insert into returnRecordSetOrderDemo values(145,'David');
使用select语句显示表中的所有记录。
查询如下。
mysql> select *from returnRecordSetOrderDemo;
以下是输出。
+------+-------+ | Id | Name | +------+-------+ | 100 | John | | 130 | Carol | | 103 | Bob | | 134 | Sam | | 102 | Larry | | 145 | David | +------+-------+ 6 rows in set (0.00 sec)
这是对MySQL'IN'子句的查询以及返回的记录集顺序。
mysql> select *from returnRecordSetOrderDemo -> where Id in(100,145,103,130) -> order by FIND_IN_SET(Id,'100,145,103,130');
以下是输出。
+------+------- | Id | Name | +------+-------+ | 100 | John | | 145 | David | | 103 | Bob | | 130 | Carol | +------+-------+ 4 rows in set (0.00 sec)
热门推荐
10 朋友新年祝福语大全 简短
11 新年祝福语简短大方兔年
12 搬新家礼物祝福语简短
13 同学见面花束祝福语简短
14 五一假期祝福语幽默简短
15 离职欢送敬酒祝福语简短
16 对学弟的祝福语简短
17 考老师辞职祝福语简短
18 祝福语驱散霉运的话简短