请问MSSQL中的row_number()功能如何实现.
初始数据:
a|b|c
4|6|5
6|5|6
5|4|4
目标结果:
A|B|C
1|3|2
3|2|3
2|1|1
MSSQL写法如下:
select ROW_NUMBER() over(order by a) as A,ROW_NUMBER() over(order by b) as B,ROW_NUMBER() over(order by c) as C from table_a求MYSQL高性能实现脚本。

解决方案 »

  1.   

    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...
      

  2.   

    mysql没有这种现成的函数  可以做一个有自增变量的临时表  把你的数据排序好以后插入这个表sqlserver2000也没有row_number这个函数的时候也是这么做的
      

  3.   

    MYSQL没有ROWNUM的函数。如果A、B、C唯一,可用查询解决,也可以用变量方法
      

  4.   

    如果A、B、C唯一SELECT *,
    (SELECT COUNT(*) FROM ttp1 WHERE a.a>=a),
    (SELECT COUNT(*) FROM ttp1 WHERE a.b>=b),
    (SELECT COUNT(*) FROM ttp1 WHERE a.c>=c)
     FROM ttp1 a
      

  5.   

    MYSQL实现分组排序( row_number()over()功能 )
    drop table if exists tb;
    create table tb(col1 int,col2 int);
    insert tb
    select 1,2 union all
    select 1,3 union all
    select 1,4 union all
    select 2,9 union all
    select 2,7 union all
    select 2,8 union all
    select 3,1 union all
    select 3,6;select  *
    from
     ( select
          a.*,
          @rownum:=@rownum+1 as 序号, 
          if(@pdept=a.col1,@rank:=@rank+1,@rank:=1) as 排名, @pdept:=a.col1  
        from
          (select * from tb order by col1 asc ,col2 desc ) a ,(select @rownum :=0 , @pdept := null ,@rank:=0)b)result;  
    /*
    Col1  col2 序号 排名 
    1 4 1 1 1
    1 3 2 2 1
    1 2 3 3 1
    2 9 4 1 2
    2 8 5 2 2
    2 7 6 3 2
    3 6 7 1 3
    3 1 8 2 3*/
      

  6.   

    变量方法:SET @a=1;
    SET @a1=0;
    SET @b=1;
    SET @b1=0;
     SET @c=1;
    SET @c1=0;SELECT a.*,b.pm,c.pm,d.pm FROM ttp1 a LEFT JOIN 
    (SELECT *,@a1:=IF(@a>a,1,@a1+1) AS pm,@a:=a
     FROM ttp1 ORDER BY a) b
     ON a.a=b.a
     LEFT JOIN
     (SELECT *,@b1:=IF(@b>b,1,@b1+1) AS pm,@b:=b
     FROM ttp1 ORDER BY b) c
     ON a.b=c.b
     LEFT JOIN
     (SELECT *,@c1:=IF(@c>c,1,@c1+1) AS pm,@c:=c
     FROM ttp1 ORDER BY c) d ON a.c=d.c;