展会信息港展会大全

Mysql select in 按id排序实现方法
来源:互联网   发布日期:2015-09-21 20:23:53   浏览:613次  

导读:有时候我们在后台选择了一系列的id,我们想安装填写id的顺序进行排序,那么就需要下面的order by方法,测试通过...

表结构如下:

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在Mysql中怎么写?

网上查到sqlserver中可以用order by charindex解决,但是没看到Mysql怎么解决??请高手帮忙,谢 谢! select * from a order by substring_index('3,1,2',id,1); 试下这个good,ls正解。

order by find_in_set(id,'3,1,5') 谢谢,经测试order by substring_index和order by find_in_set都可以

赞助本站

人工智能实验室

相关热词: Mysql select_in

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港