mysql一个连续日期(天)次数的统计

首先声明参考:http://www.oschina.net/question/573517_118821

表定义

CREATE TABLE `date_add` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `uid` int(11) NOT NULL DEFAULT '0',
 `date` date NOT NULL,
 PRIMARY KEY (`id`),
 UNIQUE KEY `idx_ud` (`uid`,`date`)
) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=latin1

插入语句

INSERT INTO `date_add` (`id`, `uid`, `date`) VALUES

(1, 1, '2015-09-01'),

(2, 1, '2015-09-02'),

(17, 1, '2015-09-03'),

(18, 1, '2015-09-04'),

(3, 2, '2015-09-01'),

(4, 2, '2015-09-02'),

(19, 2, '2015-09-03'),

(20, 2, '2015-09-04'),

(5, 3, '2015-09-01'),

(6, 3, '2015-09-02'),

(21, 3, '2015-09-03'),

(22, 3, '2015-09-04'),

(7, 4, '2015-09-01'),

(8, 4, '2015-09-02'),

(9, 5, '2015-09-01'),

(10, 5, '2015-09-02'),

(11, 6, '2015-09-01'),

(12, 6, '2015-09-02'),

(13, 7, '2015-09-01'),

(14, 7, '2015-09-02'),

(15, 8, '2015-09-01'),

(16, 8, '2015-09-02');

表如下

id uid date

1 1 2015-09-01

2 1 2015-09-02

17 1 2015-09-03

18 1 2015-09-05

3 2 2015-09-01

4 2 2015-09-02

19 2 2015-09-03

20 2 2015-09-04

5 3 2015-09-01

6 3 2015-09-02

21 3 2015-09-03

22 3 2015-09-04

7 4 2015-09-01

8 4 2015-09-02

9 5 2015-09-01

10 5 2015-09-02

11 6 2015-09-01

12 6 2015-09-02

13 7 2015-09-01

14 7 2015-09-02

15 8 2015-09-01

16 8 2015-09-02


首先,sql如下

select uid,`date`,@countday:=(case when(@last_uid:=uid and DATEDIFF(`date`,@last_date)=1)then (@countday+1)else 1 end ) as countday ,(@group_id:=(@group_id+if(@countday=1,1,0))) asgroup_id,@last_uid:=uid as last_uid,@last_date:=`date` as last_date from (select `uid`,`date` from date_add order by uid,`date` )as t1,(select@countday:=0,@group_id:=0,@last_uid:='',@last_date:='') as t2

select uid,`date`,@countday:=(case when(@last_uid:=uid and DATEDIFF(`date`,@last_date)=1)then (@countday+1)else 1 end ) as countday ,(@group_id:=(@group_id+if(@countday=1,1,0))) asgroup_id,@last_uid:=uid as last_uid,@last_date:=`date` as last_date from (select `uid`,`date` from date_add order by uid,`date` )as t1,(select@countday:=0,@group_id:=0,@last_uid:='',@last_date:='') as t2

结果

uid date countday group_id last_uid last_date

1 2015-09-01 1 1 1 2015-09-01

1 2015-09-02 2 1 1 2015-09-02

1 2015-09-03 3 1 1 2015-09-03

1 2015-09-05 1 2 1 2015-09-05

2 2015-09-01 1 3 2 2015-09-01

2 2015-09-02 2 3 2 2015-09-02

2 2015-09-03 3 3 2 2015-09-03

2 2015-09-04 4 3 2 2015-09-04

3 2015-09-01 1 4 3 2015-09-01

3 2015-09-02 2 4 3 2015-09-02

3 2015-09-03 3 4 3 2015-09-03

3 2015-09-04 4 4 3 2015-09-04

4 2015-09-01 1 5 4 2015-09-01

4 2015-09-02 2 5 4 2015-09-02

5 2015-09-01 1 6 5 2015-09-01

5 2015-09-02 2 6 5 2015-09-02

6 2015-09-01 1 7 6 2015-09-01

6 2015-09-02 2 7 6 2015-09-02

7 2015-09-01 1 8 7 2015-09-01

7 2015-09-02 2 8 7 2015-09-02

8 2015-09-01 1 9 8 2015-09-01

8 2015-09-02 2 9 8 2015-09-02


然后,sql

select uid,min(date) as mindate , max(date) as maxdate,max(countday) countday,group_concat(date) dates from ( select uid,`date`,@countday:=(case when(@last_uid:=uid andDATEDIFF(`date`,@last_date)=1)then (@countday+1)else 1 end ) as countday ,(@group_id:=(@group_id+if(@countday=1,1,0))) as group_id,@last_uid:=uid as last_uid,@last_date:=`date` aslast_date from (select `uid`,`date` from date_add order by uid,`date` )as t1,(select @countday:=0,@group_id:=0,@last_uid:='',@last_date:='') as t2) as t3 group by group_id

select uid,min(date) as mindate , max(date) as maxdate,max(countday) countday,group_concat(date) dates from ( select uid,`date`,@countday:=(case when(@last_uid:=uid andDATEDIFF(`date`,@last_date)=1)then (@countday+1)else 1 end ) as countday ,(@group_id:=(@group_id+if(@countday=1,1,0))) as group_id,@last_uid:=uid as last_uid,@last_date:=`date` aslast_date from (select `uid`,`date` from date_add order by uid,`date` )as t1,(select @countday:=0,@group_id:=0,@last_uid:='',@last_date:='') as t2) as t3 group by group_id

最终结果(countday 连续天数)

uid mindate maxdate countday dates

1 2015-09-01 2015-09-03 3 2015-09-01,2015-09-02,2015-09-03

1 2015-09-05 2015-09-05 1 2015-09-05

2 2015-09-01 2015-09-04 4 2015-09-03,2015-09-04,2015-09-01,2015-09-02

3 2015-09-01 2015-09-04 4 2015-09-03,2015-09-04,2015-09-01,2015-09-02

4 2015-09-01 2015-09-02 2 2015-09-02,2015-09-01

5 2015-09-01 2015-09-02 2 2015-09-01,2015-09-02

6 2015-09-01 2015-09-02 2 2015-09-01,2015-09-02

7 2015-09-01 2015-09-02 2 2015-09-01,2015-09-02

8 2015-09-01 2015-09-02 2 2015-09-01,2015-09-02


你可能感兴趣的:(mysql一个连续日期(天)次数的统计)