比如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多行)。
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多行)。
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
通过程序是指用php文件之类的?
存储过程能具体写出来吗?
+------+------+
| 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>