现有表(rank字段暂时为空)
ID    count      rank
1        6   
2        3 
3        7  
4        5  
...        ...怎样根据 count 字段的大小用排序并用数字表示(最大为1,依次往后)insert into 到表的rank 字段?即有这样的效果:
ID    count      rank
1        6             2
2        3             4
3        7             1  
4        5             3
...        ...
求大神指点迷津!! 

解决方案 »

  1.   

    建立一个临时表create table tem(idx int auto_increment primary key,id int,count int);insert into tem(id,count) select id,count from tb order by count desc;update tem A, tb B set B.rank = A.idx where A.id = B.id;drop table tem;
      

  2.   

    http://blog.csdn.net/acmain_chm/article/details/4095531
    MySQL中的ROWNUM的实现
    MySQL 几乎模拟了 Oracle,SQL Server等商业数据库的大部分功能,函数。但很可惜,到目前的版本(5.1.33)为止,仍没有实现ROWNUM这个功能。 下面介绍几种具体的实现方法.建立实验环境如下mysql> create table tbl (    ->  id      int primary key,    ->  col     int    -> );Que...
      

  3.   

    自己解决了!代码可这么写:
    declare Pcount int;-- 记录当前最大count值
    declare Incre int;-- 记录update影响语句
    set Prank=1;-- 排序值
    while exists(select * from table1 where rank=null) do
       select max(count) into  Pcount from table1 where rank=null;
       
       set @sql=concat("update table1 set rank=",Prank," where count=",Pcount,";");
       prepare rank from @sql;
       execute rank;
       
       select row_count() into Icre;-- 统计update影响语句
         
        set Prank=Prank+Icre;
    end while;楼上的回复有点抽象,是小弟没描述清楚?!