MySql中关于某列中相同数值连续出现次数的统计
MySql中關(guān)于某列中相同數(shù)值連續(xù)出現(xiàn)次數(shù)的統(tǒng)計(jì)
?
原表如下: www.2cto.com ?
100
101
102
100
100
103
104
102
102
105
106
101
101
輸出如下: ?www.2cto.com ?
100 ? ?1
101 ? ?2
102 ? ?3
100 ? ?4
100 ? ?4
103 ? ?5
104 ? ?6
102 ? ?7
102 ? ?7
105 ? ?8
106 ? ?9
101 ? ?10
101 ? ?10
sql如下:
SET @t1=0;
SET @tp=-1;
select ??
? @t1 := @t1 + (case when @tp=n then 0 else 1 end) as c,?
? n,
? @tp := n
from nums
order by n;
?
測(cè)試數(shù)據(jù):
create table nums( n int );
insert into nums values (100), (101), (101), (102);
?
輸出結(jié)果:
| C | ? N | @TP := N |
----------------------
| 1 | 100 | ? ? ?100 |
| 2 | 101 | ? ? ?101 |
| 2 | 101 | ? ? ?101 |
| 3 | 102 | ? ? ?102 |
?
?
create table nums( n int );
insert into nums values (100), (101), (101), (102);
SET @t1=0;
 SET @tp=-1;
 select @t1 := @t1 + (case when @tp=n then 0 else 1 end) as c, n, @tp := n
 from nums GROUP BY n order by n ;
?
輸出結(jié)果:
| C | ? N | @TP := N |
----------------------
| 1 | 100 | ? ? ?100 |
| 2 | 101 | ? ? ?101 |
| 3 | 102 | ? ? ?102 |
?
總結(jié)
以上是生活随笔為你收集整理的MySql中关于某列中相同数值连续出现次数的统计的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
 
                            
                        - 上一篇: mysql中limit用法
- 下一篇: MySQL的一些简单语句
