首页 > 数据库技术 > 详细

SQL 按指定顺序进行排序

时间:2015-11-02 13:43:37      阅读:259      评论:0      收藏:0      [点我收藏+]

在有些情况下我们需要按指定顺序输出数据,比如选择了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,‘)

SQL 按指定顺序进行排序

原文:http://www.cnblogs.com/lovton/p/4929810.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!