SQL语句之按in排序
2015-06-19 10:21
337 查看
有时候我们需要按照in条件里的id顺序输出结果,可sql语句在不加order by的时候是按照asc排序的,下边的sql解决按照in条件顺序的id输出查询结果
mysql写法:
SELECT * FROM EVENT WHERE eventId IN(443,419,431,440,420,414,509) ORDER BY INSTR(',443,419,431,440,420,414,509,',CONCAT(',',eventId,','))
oracle写法:
select name from order where oderid in(111,222,333,444,555,666)order by instr('111,222,333,444,555,666',orderid)
sqlserver写法:
Select * From Product Where id in (1,1588,15782,9887,54) Order By charindex(','+ id +',', ',1,1588,15782,9887,54,')
Access:
select * From 表 Where id in(1,5,3) order by instr(',1,5,3,',','&id&',')
MSSQL:
select * From 表 Where id in(1,5,3) order by charindex(','+rtrim(cast(id as varchar(10)))+',',',1,5,3,')
MySQL:
select * From 表 Where id in (1,5,3) order by instr(',1,5,3,',',CONCAT(',',id,','))
mysql写法:
SELECT * FROM EVENT WHERE eventId IN(443,419,431,440,420,414,509) ORDER BY INSTR(',443,419,431,440,420,414,509,',CONCAT(',',eventId,','))
oracle写法:
select name from order where oderid in(111,222,333,444,555,666)order by instr('111,222,333,444,555,666',orderid)
sqlserver写法:
Select * From Product Where id in (1,1588,15782,9887,54) Order By charindex(','+ id +',', ',1,1588,15782,9887,54,')
Access:
select * From 表 Where id in(1,5,3) order by instr(',1,5,3,',','&id&',')
MSSQL:
select * From 表 Where id in(1,5,3) order by charindex(','+rtrim(cast(id as varchar(10)))+',',',1,5,3,')
MySQL:
select * From 表 Where id in (1,5,3) order by instr(',1,5,3,',',CONCAT(',',id,','))
相关文章推荐
- Oracle 常见等待事件
- Oracle 空间管理
- [笔记]数据库系统设计之命名与主键选择
- [笔记]数据库系统设计之命名与主键选择
- MySQL 强制索引
- MySQL 的执行计划
- Oracle 索引
- mysql varchar类型转换日期类型,比较大小
- VCenter 5.5 数据库过大造成的问题和解决方法
- 数据库资源消耗高时两条简单管用、快速找出可能问题原因的SQL语句
- Oracle中使用profile管理用户口令
- oracle分页查询
- Mysql常用命令
- SQL Server 系统表介绍:dm_exec_connections
- MySQL进口.sql文件和常用命令
- MySQL插入数据时,如果记录不存在则insert,如果存在则update
- Oracle用户管理
- WINCE平台安装sqlCE
- SQL Server 系统表介绍:sys.dm_exec_sessions
- Oracle 查找带有CLOB字段的所有表