暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

SQL基础-order by排序

SQL数据库笔记 2019-11-19
683

在进行数据查询时,可以使用order by子句对查询的结果按照一个列或多个列进行排序。

特点:

1.多列的时候,先按照第一个column_name排序,再按照第二个column_name排序。

2.order by 排列时,不写明asc、desc的时候,默认是asc。

order by 的语法:

select column_name1,column_name2,column_name3,...
from table_name
order by order_expression asc|desc
--order_expression指定排序列,排序列之间用逗号隔开
--asc升序排列,desc降序排列

示例:

表的定义如下:

create table [dbo].[StuScores](
[StuId] [int] not null,
[CusId] [int] not null,
[Score] [float] not null,
[TestDate] [datetime] null
)

1.普通查询如下:

select * from  [dbo].[StuScores]

2.使用order by 对分数进行升序排列:

select * from  [dbo].[StuScores] order by [Score] asc

这里对分数进行了升序排列

3.对分数进行排序,默认排序方式

select * from  [dbo].[StuScores] order by [Score]

默认方式是升序排列

4.使用order by 对分数和学号进行升序排列:

select * from  [dbo].[StuScores] order by [Score],[StuId] asc

先根据分数进行升序排列后根据学号升序排列

5.使用order by 对分数和学号进行降序排列:

select * from  [dbo].[StuScores]
order by [Score] asc,[StuId] desc

先根据分数进行升序排列后根据学号降序排列

end

如有错误欢迎留言指正

有兴趣的小伙伴可以关注“SQL数据库笔记”公众号,一起学习吧!


最后修改时间:2019-12-20 16:40:18
文章转载自SQL数据库笔记,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论