入门客AI创业平台(我带你入门,你带我飞行)
博文笔记

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

创建时间:2013-02-20 投稿人: 浏览次数:2874

原表如下:

100
101
102
100
100
103
104
102
102
105
106
101
101

输出如下:

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 |

原文见:http://stackoverflow.com/questions/12285926/mysql-consecutive-column
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
  • 上一篇:没有了
  • 下一篇:没有了
未上传头像