获取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 官宣新年祝福语简短