数据库简单查询学生成绩降序排列 desc是升序还是降序?

[更新]
·
·
分类:互联网
3418 阅读

数据库简单查询学生成绩降序排列

desc是升序还是降序?

desc是升序还是降序?

在sql中,desc是降序排列。
而升序排列是使用asc,同时sql默认的排序也是升序排列。desc在sql中的用法是对查询出的结果按某一列来降序排序,在使用的时候,要和order by 一起使用。
用法举例:
以Access数据库为例,现有一张学生信息表Student,含有name(姓名),age(年龄),gender(性别),grade(班级),score(成绩)这几个字段。
name age gender grade score
李四 19 男 高三6班 62
马六 20 男 高三4班 77
王五 18 女 高三1班 93
小明 19 男 高三2班 53
张三 18 男 高三2班 88
select * from Student order by age//按年龄升序查询所有学生 查询结果如下:
name age gender grade score
王五 18 女 高三1班 93
张三 18 男 高三2班 88
小明 19 男 高三2班 53
李四 19 男 高三6班 62
马六 20 男 高三4班 77
可以看出查询结果的学生按年龄升序进行了排列。
select * from Student order by score desc//按成绩降序查询所有学生 查询结果如下:
name age gender grade score
王五 18 女 高三1班 93
张三 18 男 高三2班 88
小明 19 男 高三2班 53
李四 19 男 高三6班 62
马六 20 男 高三4班 77
在使用desc也可以通过 , 来隔开多个排序条件:
select * from Student order by age desc,score desc//先根据年龄排序,其次通过分数排序 查询结果如下:
name age gender grade score
李四 17 男 高三6班 62
王五 18 女 高三1班 93
小明 19 男 高三2班 53
马六 20 男 高三4班 77
小花 21 女 高三1班 90
张三 21 男 高三2班 88
可以看出查询结果中,是首先按年龄来降序排序,年龄相同时,如小花与张三,则按分数降序排列。

sqldesc怎么用?

sqldesc使用方法
sql中的排序使用倒序的步骤如下:
我们需要准备的材料分别是:电脑、sql查询器。
1、首先,打开sql查询器,连接上相应的数据库表,例如test表,以score字段倒序为例。
2、点击“查询”按钮,输入:select * from test order by score desc。
3、点击“运行”按钮,此时会发现score字段按倒序排序查询出了。
如何用sql语句排序一个倒一个顺 比如 az 这个字段是顺序 asc id这个字
例如,按学生学号升序排列,学生成绩按降序排列
sql是这样写的:select * from tab order by id,scroe desc
sql server会根据order by跟id scroe 先后进行排序,
先根据id升序排序,再根据scroe降序排序,也许你会发现scroe列的数据不是按照降序排列
这就是优先排序的原则,order by 后面谁在前,谁就优先排序
你可以仔细看看相同的id(你可以插入几行相同的id,不同scroe),score就是按照降序排列的
sql 升序降序排列
降序:SELECT * FROM kc ORDER BY cpbh DESC
升序:SELECT * FROM kc ORDER BY cpbh ASC
语法:
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 默认为升序*/
扩展资料:
一、ORDER BY 语句
ORDER BY 语句用于根据指定的列对结果集进行排序。
ORDER BY 语句默认按照升序对记录进行排序。
如果您希望按照降序对记录进行排序,可以使用 DESC 关键字。
二、SQL 排序多个字段
order by 多个字段,每个字段后面都有排序方式,默认ASC
例如:select table a order by a.time1 ,a.time2 desc,a.time3 asc
参考资料:w3school-SQL ORDER BY 子句
SQL语句返回排序后的位置
可惜SQL SERVER没有直接查看结果集中某行所在位置的功能,只能借助临时表了。以下语句同时执行:
if exists(select * from sysobjects where name #39temp_for_insert#39 )
begin
drop table temp_for_insert
end
select identity(int,1,1) as rowid,id,name,addtime
into temp_for_insert
from [users]
where addtime between #3920061129#39 and #3920061130#39
order by addtime
select *
from temp_for_insert
drop table temp_for_insert
注意,如果users表中原来有自增的列的话,需要在select into的时候转换一下,比如id列是自增的,语句就是
select identity(int,1,1) as rowid,cast(id as int) as id,name,addtime
into temp_for_insert
from [users]
where addtime between #3920061129#39 and #3920061130#39
order by addtime
where addtime between #3920061129#39 and #3920061130#39
是限定时间段的。