zl程序教程

您现在的位置是:首页 >  后端

当前栏目

Mysqlselectin按id排序实现方法

方法排序 实现 ID
2023-06-13 09:14:47 时间
表结构如下:
mysql>select*fromtest;
+----+-------+
|id|name|
+----+-------+
|1|test1|
|2|test2|
|3|test3|
|4|test4|
|5|test5|
+----+-------+

执行以下SQL:
mysql>select*fromtestwhereidin(3,1,5);
+----+-------+
|id|name|
+----+-------+
|1|test1|
|3|test3|
|5|test5|
+----+-------+
3rowsinset(0.00sec)

这个select在mysql中得结果会自动按照id升序排列,
但是我想执行"select*fromtestwhereidin(3,1,5);"的结果按照in中得条件排序,即:3,1,5,

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

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

谢!

select*fromaorderbysubstring_index("3,1,2",id,1);

试下这个good,ls正解。


orderbyfind_in_set(id,"3,1,5")

谢谢,经测试orderbysubstring_index和orderbyfind_in_set都可以