当前位置:首页 » 编程语言 » sql查询序列

sql查询序列

发布时间: 2024-11-03 16:36:54

sql 升序降序排列

降序:SELECT * FROM kc ORDERBYcpbh DESC

升序:SELECT * FROM kc ORDERBYcpbhASC

语法:

sql可以根据字段进行排序,其中,DESC表示降序,ASC表示升序

order by 字段名 DESC;按照字段名降序排序

order by 字段名 ASC;按照字段名升序排序

实例:

一、/*查询学生表中姓名、学号,并以学号降序排序*/

select name,StuID from Students_information order by StuID desc /**order by 以什么排序,默认为升序,desc是降序*/

二、/*查询学生表中前5名学生的姓名,学号,并以学号升序排列*/

select top 5 name,StuID from Students_information order by StuID /*order by 默认为升序*/

(1)sql查询序列扩展阅读

一、ORDER BY 语句

ORDER BY 语句用于根据指定的列对结果集进行排序。

ORDER BY 语句默认按照升序对记录进行排序。

如果您希望按照降序对记录进行排序,可以使用 DESC 关键字。

二、SQL 排序多个字段

order by 多个字段,每个字段后面都有排序方式,默认ASC

例如:select table a order by a.time1 ,a.time2 desc,a.time3 asc

② 在SQL中,如何查询结果中某条记录的序列

use Tempdb
go
--> -->

declare @T table([id] int,[name] nvarchar(1),[count] int)
Insert @T
select 4,N'A',18 union all
select 5,N'B',19 union all
select 6,N'A',19 union all
select 7,N'A',20
--SQL2000
Select
[ID]=(select count(distinct [count]) from @T where [id]<=t.[id]),
[name],[count]
from @T t
where not exists(select 1 from @t where [count]=t.[count] and [id]>t.[id])

--SQL2005
select
ID=row_number()over(order by ID),
[name],[count]
from @T t
where not exists(select 1 from @t where [count]=t.[count] and [id]>t.[id])

(4 个资料列受到影响)
ID name count
----------- ---- -----------
1 A 18
2 A 19
3 A 20

(3 个资料列受到影响)

ID name count
-------------------- ---- -----------
1 A 18
2 A 19
3 A 20

(3 个资料列受到影响)

热点内容
用什么缓存视频 发布:2025-07-13 14:52:27 浏览:360
GTA5mods文件夹 发布:2025-07-13 14:52:27 浏览:52
如何在本地搭建svn服务器地址 发布:2025-07-13 14:52:26 浏览:963
手动编程教程 发布:2025-07-13 14:52:17 浏览:377
编程要求学历 发布:2025-07-13 14:50:40 浏览:186
投资黄金源码 发布:2025-07-13 14:48:49 浏览:881
androidservice的生命周期 发布:2025-07-13 14:44:24 浏览:669
c语言另存 发布:2025-07-13 14:42:29 浏览:279
脚本的写作 发布:2025-07-13 14:42:27 浏览:80
python多行匹配 发布:2025-07-13 14:42:14 浏览:47