zoukankan      html  css  js  c++  java
  • SQL 按指定顺序进行排序

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

  • 相关阅读:
    UVa 106
    UVa 111
    UVa 105
    UVa 104
    UVa 103
    UVa 102
    UVa 101
    UVa 100
    就决定是你了!
    阿姆斯特朗回旋加速喷气式阿姆斯特朗炮
  • 原文地址:https://www.cnblogs.com/lovton/p/4929810.html
Copyright © 2011-2022 走看看