root@127.0.0.1 : test > select * from itpub1;
+------+------+
| id | name |
+------+------+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 1 |
| 5 | 2 |
| 6 | 2 |
| 7 | 1 |
| 8 | 1 |
+------+------+
8 rows in set (0.00 sec)
root@127.0.0.1 : test > select aa.name,max(rownum) maxrow from ( select ib.id,ib.name,if((ib.name=@pname) , @row:=@row+1 , @row:=1) rownum , @pname:=ib.name pn from itpub1 ib, (select @row:=0, @pname:='000000') row1 ) aa group by aa.name;
+------+--------+
| name | maxrow |
+------+--------+
| 1 | 4 |
| 2 | 2 |
+------+--------+
+------+------+
| id | name |
+------+------+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 1 |
| 5 | 2 |
| 6 | 2 |
| 7 | 1 |
| 8 | 1 |
+------+------+
8 rows in set (0.00 sec)
root@127.0.0.1 : test > select aa.name,max(rownum) maxrow from ( select ib.id,ib.name,if((ib.name=@pname) , @row:=@row+1 , @row:=1) rownum , @pname:=ib.name pn from itpub1 ib, (select @row:=0, @pname:='000000') row1 ) aa group by aa.name;
+------+--------+
| name | maxrow |
+------+--------+
| 1 | 4 |
| 2 | 2 |
+------+--------+
2 rows in set (0.00 sec)
--本篇文章转自:SQL--计算某一值连续出现的最大次数