SQL 关于排序

来源:百度知道 编辑:UC知道 时间:2024/06/30 08:20:41
例如有个字段IDS ,将他排序之后,加如一个字段,怎么能显示他的排名

create table test(IDS int)
insert test select 10
insert test select 40
insert test select 20
insert test select 30

select 排名=1+(select count(*) from test where IDS <x.IDS ), * from test x
order by IDS

drop table test

--结果
/*
排名 IDS
---------------
1 10
2 20
3 30
4 40
*/

用 select * from 表 order by ids desc 这样的SQL语句排序, 结果就是从大到小排列的.

然后按顺序进行显示, 显示的时候再逐条加上序号(1,2,3,4...), 就可以了.

不明白你的意思。

索引