比如1张表: 
a      b 
----------- 
str1  1 
str1  1 
str1  1 
str2  1 
str2  1 
str2  1 
str2  1 
str2  1 
str3  1 
str3  1 
我想变为 a      b 
----------- 
str1  1 
str1  2 
str1  3 
str2  1 
str2  2 
str2  3 
str2  4 
str2  5 
str3  1 
str3  2 请教用sql语句怎样能实现? 
(这里10行只是假设,实际有n多行)。

解决方案 »

  1.   

    加入自增字段ID可以解决
    select a.a,a.id,sum(b.b) from ttg7 a inner join ttg7 b 
    on a.a=b.a and a.id>=b.id group by a.a,a.id
      

  2.   

    MySQL中,如果你的表结构不做更改的话,只能通过程序来实现了。可以在你的外部程序中来更新这个字段,或者在数据库中用存储过程来实现。
      

  3.   


    通过程序是指用php文件之类的?
    存储过程能具体写出来吗?
      

  4.   

    mysql> select * from table1;
    +------+------+
    | a    | b    |
    +------+------+
    | str1 |    1 |
    | str1 |    1 |
    | str1 |    1 |
    | str2 |    1 |
    | str2 |    1 |
    | str2 |    1 |
    | str2 |    1 |
    | str2 |    1 |
    | str3 |    1 |
    | str3 |    1 |
    +------+------+
    10 rows in set (0.00 sec)mysql> set @oldA='';
    Query OK, 0 rows affected (0.00 sec)mysql> select a,@sno:=if(@oldA=a,@sno,0)+1 as b,@oldA:=a
        -> from table1
        -> order by a;
    +------+------+----------+
    | a    | b    | @oldA:=a |
    +------+------+----------+
    | str1 |    1 | str1     |
    | str1 |    2 | str1     |
    | str1 |    3 | str1     |
    | str2 |    1 | str2     |
    | str2 |    2 | str2     |
    | str2 |    3 | str2     |
    | str2 |    4 | str2     |
    | str2 |    5 | str2     |
    | str3 |    1 | str3     |
    | str3 |    2 | str3     |
    +------+------+----------+
    10 rows in set (0.00 sec)mysql>