MySql中关于某列中相同数值连续出现次数的统计_MySQL
Jun 01, 2016 pm 01:36 PMbitsCN.com
MySql中关于某列中相同数值连续出现次数的统计
原表如下: www.bitsCN.com
100
101
102
100
100
103
104
102
102
105
106
101
101
输出如下: www.bitsCN.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 |
bitsCN.com

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to implement data statistics and analysis in uniapp

How to use SQL statements for data aggregation and statistics in MySQL?

How to quickly build a statistical chart system under the Vue framework

Implementation of linear and pie chart functions in Vue statistical charts

How to use MySQL's COUNT function to count the number of rows in a data table

Use Google Analytics to count website data in Beego

Do you know how to count people in excel?
