排列顺序不固定 sql怎么写?
例如表tab 字段 ID
ID
1
2
3
4
5
.
.
.
排列顺序有可能是
2
1
4
5
3
.
.
.
也有可能是
5
3
2
1
4
.
.
.
这样SQL可以写吗?? 
.

解决方案 »

  1.   

    declare @tb table (id int)
    insert into @tb select 1
    insert into @tb select 2
    insert into @tb select 3
    insert into @tb select 4
    insert into @tb select 5
    --case when
    select * from @tb 
    order by case when id=5 then 0
    when id=3 then 1
    else 2 end,id5
    3
    1
    2
    4
      

  2.   

    随机排序:
    select * from tab order by newid()