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

热点内容
linux部署windows 发布:2025-07-13 22:53:37 浏览:261
c语言printf函数用法 发布:2025-07-13 22:53:30 浏览:293
压缩萝卜干 发布:2025-07-13 22:52:01 浏览:672
为什么dns一直配置错误 发布:2025-07-13 22:43:06 浏览:258
fortran如何编译 发布:2025-07-13 22:31:05 浏览:480
sql语句查询字段 发布:2025-07-13 22:24:20 浏览:632
python目录遍历 发布:2025-07-13 22:16:24 浏览:96
卖房说解压 发布:2025-07-13 22:06:49 浏览:216
C加密传输 发布:2025-07-13 22:06:08 浏览:162
配置不合理怎么解决 发布:2025-07-13 22:01:07 浏览:735