内容简介: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排序实现方法》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
产品经理的第二本书
[美] 哥乔斯 / 戴维侬 / 中国财政经济出版社 / 2004-4 / 39.80元
产品经理的第二本书:产品经理实战技巧指南,ISBN:9787500570950,作者:(美)琳达·哥乔斯(Linda Gorchels)著;戴维侬译;戴维侬译一起来看看 《产品经理的第二本书》 这本书的介绍吧!