Mysqlselectin按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都可以
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都可以
相关文章
- awk 数组排序多种实现方法「建议收藏」
- Array 数组的几种排序方法与常用添加数组元素方法
- C++ list(STL list)排序及合并元素方法详解
- STL priority_queue自定义排序实现方法详解
- Oracle 排序函数:实现快速排序的方法(排序函数oracle)
- MySQL排序:实现自增的方法(mysql排序自增)
- Linux排序查看文件的快速方法(linux排序查看文件)
- MySQL中汉字拼音排序实现方法(mysql汉字拼音排序)
- data排序管理Redis数据的方法(sortredis)
- 方法Oracle数据库中的自动排序方法(oracle自动排序)
- 使用Redis Set实现高效排序:探究排序方法及优化策略(redisset排序)
- Linux 中实现行排序的方法(行排序linux)
- MSSQL中实现置顶排序的方法(mssql置顶排序)
- MySQL中实现随机排序的方法(mysql随机排序)
- by排序的使用方法(mysql中order)
- 实现Redis按某字段排序的方法(redis根据某字段排序)
- js重构Array的sort排序方法
- SQL学习笔记四聚合函数、排序方法
- js实现图片放大缩小功能后进行复杂排序的方法
- java中常用的排序方法
- JavaScript实现表格排序方法
- sqlServer使用ROW_NUMBER时不排序的解决方法