数据库里有1,2,3,4,5 共5条记录,要用一条sql语句让其排序,使它排列成4,5,1,2,3,怎么写?
CREATE TABLE #AAA (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[GUID] [uniqueidentifier] NULL
) ON [PRIMARY]
GO


下面这句执行5次
insert #AAA values (newid())

查看执行结果
select * from #AAA

1、 第一种
select * from #AAA
order by case id when 4 then 1
when 5 then 2
when 1 then 3
when 2 then 4
when 3 then 5 end

2、 第二种
select * from #AAA order by (id+2)%6

3、 第三种
select * from #AAA order by charindex(cast(id as varchar),'45123')

4、 第四种
select * from #AAA
WHERE id between 0 and 5
order by charindex(cast(id as varchar),'45123')

5、 第五种
select * from #AAA order by case when id >3 then id-5 else id end

6、 第六种
select * from #AAA order by id / 4 desc,id asc
相关文章:
-
2022-12-23
-
2022-12-23
-
2022-12-23
-
2021-10-21
-
2022-12-23
-
2022-12-23
-
2021-12-31
-
2021-11-15
猜你喜欢
-
2021-12-31
-
2022-12-23
-
2022-01-28
-
2022-12-23
-
2022-01-13
-
2022-12-23
-
2022-12-23
相关资源
-
下载
2021-06-05
-
下载
2022-12-02
-
下载
2021-06-27
-
下载
2022-12-15
-
下载
2021-06-27