Mysql select in 按id排序实现方法

栏目: 数据库 · Mysql · 发布时间: 8年前

内容简介:Mysql select in 按id排序实现方法

Mysql select in 按id排序实现方法

表结构如下: 

mysql> select * from test; 
+----+-------+ 
| id | name | 
+----+-------+ 
| 1 | test1 | 
| 2 | test2 | 
| 3 | test3 | 
| 4 | test4 | 
| 5 | test5 | 
+----+-------+ 

执行以下SQL: 

mysql> select * from test where id in(3,1,5); 
+----+-------+ 
| id | name | 
+----+-------+ 
| 1 | test1 | 
| 3 | test3 | 
| 5 | test5 | 
+----+-------+ 
3 rows in set (0.00 sec) 

这个select在 mysql 中得结果会自动按照id升序排列, 

但是我想执行 "select * from test where id in(3,1,5);" 的结果按照in中得条件排序,即:3,1,5, 

想得到的结果如下: 

id name 
3 test3 
1 test1 
5 test5 

sql语句可以更改为:

select * from test where id in(3,1,5) order by find_in_set(id,'3,1,5') 

经测试order by substring_index和order by find_in_set都可以


以上所述就是小编给大家介绍的《Mysql select in 按id排序实现方法》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Building Social Web Applications

Building Social Web Applications

Gavin Bell / O'Reilly Media / 2009-10-1 / USD 34.99

Building a social web application that attracts and retains regular visitors, and gets them to interact, isn't easy to do. This book walks you through the tough questions you'll face if you're to crea......一起来看看 《Building Social Web Applications》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具