当前位置:首页 » 编程语言 » 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 个资料列受到影响)

热点内容
升级fw用ftp服务器 发布:2025-03-16 23:27:35 浏览:343
汽车安全配置哪些好 发布:2025-03-16 23:16:42 浏览:174
vcmfc源码 发布:2025-03-16 23:14:17 浏览:503
如何设置禁止访问服务器ip 发布:2025-03-16 23:14:07 浏览:499
linuxloadrunner 发布:2025-03-16 23:12:18 浏览:765
搭建fms服务器 发布:2025-03-16 23:11:27 浏览:978
代码编程图片 发布:2025-03-16 23:09:58 浏览:412
研发加密 发布:2025-03-16 23:09:51 浏览:609
哪些车有配置前后防撞钢梁 发布:2025-03-16 22:55:35 浏览:729
服务器怎么设置外网访问 发布:2025-03-16 22:53:03 浏览:186