Home > Database > Mysql Tutorial > MySql中关于某列中相同数值连续出现次数的统计

MySql中关于某列中相同数值连续出现次数的统计

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-07 15:08:17
Original
2810 people have browsed it

MySql中关于某列中相同数连续出现次数的统计 原表如下: 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

MySql中关于某列中相同数值连续出现次数的统计

 

原表如下: 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;

 

测试数据:

create table nums( n int );

insert into nums values (100), (101), (101), (102);

 

输出结果:

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

 

输出结果:

| C |   N | @TP := N |

----------------------

| 1 | 100 |      100 |

| 2 | 101 |      101 |

| 3 | 102 |      102 |

 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
MySQL stops process
From 1970-01-01 08:00:00
0
0
0
Error when installing mysql on linux
From 1970-01-01 08:00:00
0
0
0
phpstudy cannot start mysql?
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template