SQL 按指定顺序进行排序
2015-11-02 12:14
288 查看
在有些情况下我们需要按指定顺序输出数据,比如选择了ID in(3,1,2,5,4)我们希望按这个3,1,2,5,4的顺序输出,这样只使用order by ID是无法实现的,但是我们可以使用order by charindex(','+convert(varchar,ID)+',',',3,1,2,5,4,')的方法来实现这个目的。举例如下:
Create Table info(
ID int identity(1,1) not null,
title varchar(100) not null
)
select id,title
from info
where id in ('3,1,2,5,4')
order by charindex(','+convert(varchar,ID)+',',',3,1,2,5,4,')
Create Table info(
ID int identity(1,1) not null,
title varchar(100) not null
)
select id,title
from info
where id in ('3,1,2,5,4')
order by charindex(','+convert(varchar,ID)+',',',3,1,2,5,4,')
相关文章推荐
- mysql分表
- 【Oracle】Oracle ora-28056 、 ORA-01034 、 ORA-27101问题解决
- PostgreSQL的MVCC并发处理
- (转)使用Migrations更新数据库结构(Code First )
- Mysql分库分表
- 恢复RMAN备份时遇到ORA-01180错误
- php memcache 使用学习
- TSQL “匹配全部”语义的实现
- 一行代码玩转android数据库之增删改查,超级简单 第三方框架xUtils改写 简单实用
- toad for sqlserver5.7
- SQL Server 查询性能优化 相关文章
- MyBatis+MySQL 返回插入的主键ID
- Windows Server 2008 R2 Enterprise 上用 SqlServer 2008 R2 创建发布出现异常
- PASSION之Mysql集群(二)
- c#读取excel数据,同时存入mysql数据库
- MySQL 5.6 for Windows 解压缩版配置安装
- 【XAMPP】mysql数据库忘记管理员root密码
- sqlite3
- mysql分库、分表、分区
- SQL分页