加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

MYSQL教程Mysql select in 按id排序实现方法

发布时间:2020-12-12 01:06:21 所属栏目:MySql教程 来源:网络整理
导读:《MYSQL教程Mysql select in 按id排序实现方法》要点: 本文介绍了MYSQL教程Mysql select in 按id排序实现方法,希望对您有用。如果有疑问,可以联系我们。 表结构如下: mysql select * from test; +----+-------+ | id | name | +----+-------+ | 1 | test1

《MYSQL教程Mysql select in 按id排序实现方法》要点:
本文介绍了MYSQL教程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,5);"的结果按照in中得条件排序,即:3,5,

想得到的结果如下:
id name
3 test3
1 test1
5 test5

请问在这样的SQL在Mysql中怎么写?
网上查到sqlserver中可以用order by charindex解决,但是没看到Mysql怎么解决??请高手帮忙,谢

谢!

select * from a order by substring_index('3,2',id,1);

试下这个good,ls正解.


order by find_in_set(id,'3,5')

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读