数据库排序问题(写出SQL语句)

2025-02-28 15:32:41
推荐回答(5个)
回答1:

将字段依次写在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

回答2:

不规则也是一种规则。
如果你对输出结果的顺序有要求,就按fibona和_wangsen 的方法可以实现;
如果只是希望按照随机顺序输出,可以用以下语句
select ID from tablename order by newid()

回答3:

简单的:再增加一个排序字段,主要让它给ID字段排序
select id from [table] order by [排序字段]
ID:4,7,6,5,3,8,2,9,1
[排序字段] :1,2,3,4,5,6,7,8,9

回答4:

加一个字段showorder并指定排序字段为showorer
然后把对应Id的showorder的顺序指定,然后按照showorder字段排序。就是你要的排序了。

回答5:

想不规则排序,可以使用随机排序
select ID from table order by newID()

有疑问可以HI聊:)