id 级别 当前经验值 当前级别经验值
1    5     2113         5000
2    6     4528         6000
3    5     1002         5000
4    3     2999         3000
5    5     3958         5000
……
……
这样的数据怎么写排行语句呢,新手提问,麻烦高手指点一下。排行后正确的顺序:id 级别 当前经验值 当前级别经验值
2    6     4528         6000
5    5     3958         5000
1    5     2113         5000
3    5     1002         5000
4    3     2999         3000

解决方案 »

  1.   

    select * from tb order by 当前级别经验值 desc , 当前经验值 desc
      

  2.   

    create table tb(id int,级别 int,当前经验值 int,当前级别经验值 int)
    insert into tb values(1 ,5 ,2113 ,5000)
    insert into tb values(2 ,6 ,4528 ,6000)
    insert into tb values(3 ,5 ,1002 ,5000)
    insert into tb values(4 ,3 ,2999 ,3000)
    insert into tb values(5 ,5 ,3958 ,5000)
    goselect * from tb order by 当前级别经验值 desc , 当前经验值 desc
    /*
    id          级别          当前经验值       当前级别经验值     
    ----------- ----------- ----------- ----------- 
    2           6           4528        6000
    5           5           3958        5000
    1           5           2113        5000
    3           5           1002        5000
    4           3           2999        3000(所影响的行数为 5 行)
    */select * from tb order by 级别 desc , 当前级别经验值 desc , 当前经验值 desc
    /*
    id          级别          当前经验值       当前级别经验值     
    ----------- ----------- ----------- ----------- 
    2           6           4528        6000
    5           5           3958        5000
    1           5           2113        5000
    3           5           1002        5000
    4           3           2999        3000(所影响的行数为 5 行)
    */
    drop table tb
      

  3.   

    SELECT * FROM tB ORDER BY 当前级别经验值 DESC,当前经验值 DESC是这样吗?
      

  4.   

    老大 实在抱歉,我补充一下,需要得到名次,数据显示会做。select TopNo=(select count(级别)+1 from 表 where 级别 > b.级别) from 表 b where 当前经验 > 0 and id=" & id这是自己写的,结果是错误的……
      

  5.   

    --这样?
    create table tb(id int,级别 int,当前经验值 int,当前级别经验值 int)
    insert into tb values(1 ,5 ,2113 ,5000)
    insert into tb values(2 ,6 ,4528 ,6000)
    insert into tb values(3 ,5 ,1002 ,5000)
    insert into tb values(4 ,3 ,2999 ,3000)
    insert into tb values(5 ,5 ,3958 ,5000)
    goselect 名次 = (select count(1) from tb where 当前级别经验值 > t.当前级别经验值 or (当前级别经验值 = t.当前级别经验值 and 当前经验值 > t.当前经验值)) +1, * from tb t order by 名次
    /*
    名次          id          级别          当前经验值       当前级别经验值     
    ----------- ----------- ----------- ----------- ----------- 
    1           2           6           4528        6000
    2           5           5           3958        5000
    3           1           5           2113        5000
    4           3           5           1002        5000
    5           4           3           2999        3000(所影响的行数为 5 行)
    */select 名次 = (select count(1) from tb where 级别 > t.级别 or (当前级别经验值 > t.当前级别经验值 or (当前级别经验值 = t.当前级别经验值 and 当前经验值 > t.当前经验值))) +1 , * from tb t order by 名次
    /*
    名次          id          级别          当前经验值       当前级别经验值     
    ----------- ----------- ----------- ----------- ----------- 
    1           2           6           4528        6000
    2           5           5           3958        5000
    3           1           5           2113        5000
    4           3           5           1002        5000
    5           4           3           2999        3000(所影响的行数为 5 行)
    */
    drop table tb
      

  6.   

    其实如果是2005,很简单,使用row_number即可.select 名次 = row_number() over(order by 当前级别经验值 desc , 当前经验值 desc), * from tb t order by 名次select 名次 = row_number() over(order by 级别 desc ,当前级别经验值 desc , 当前经验值 desc), * from tb t order by 名次