当前位置:首页 » 编程语言 » 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-03-16 22:17:48 浏览:180
linuxc函数库 发布:2025-03-16 22:03:33 浏览:921
iphone最新版系统从哪里改密码 发布:2025-03-16 21:56:19 浏览:596
python的execute 发布:2025-03-16 21:40:24 浏览:767
今天的访问量就靠你了 发布:2025-03-16 21:39:35 浏览:430
linux分区表查看 发布:2025-03-16 21:20:17 浏览:645
java多个if 发布:2025-03-16 21:15:46 浏览:696
可乐存储 发布:2025-03-16 21:15:07 浏览:873
ios迁移安卓用什么助手 发布:2025-03-16 20:12:42 浏览:720
python异常值处理 发布:2025-03-16 20:12:42 浏览:581