數據庫排序問題(寫出SQL語句)
將字段依次寫在order by 后面即可 , 中間用逗號隔開。
view plaincopy to clipboardprint?
select * from 表 order by time , name
select * from 表 order by time asc , name asc
select * from 表 order by time desc , name desc
select * from 表 order by time asc , name desc
select * from 表 order by time desc , name asc(注: asc 表示升序 , desc表示降序 , 未明確寫明排序方式時默認是升序)
與之類似的語法是 group by , 按多個字段分組時 , 也是依次將多個字段寫在group by 的后面 , 并用逗號隔開 , 范例如下:
view plaincopy to clipboardprint?
select time , name , sum(*) from 表 group by time , name
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 *1 ,*2 desc,*3 asc
參考資料:w3school-SQL ORDER BY 子句