mysql查询满足条件的连续时间_Mysql如何查询连续的时间次数

在网上看到一道有意思的题目,大意是如何在mysql查询连续的时间内登录的次数。原文链接:

首先建表,填充测试数据:

CREATE TABLE `tmysql_test_lianxu_3` (

`id` int(11) NOT NULL AUTO_INCREMENT,

`uid` int(11) DEFAULT NULL,

`sts` datetime DEFAULT NULL COMMENT '登录时间',

`ets` datetime DEFAULT NULL COMMENT '离线时间',

PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 COLLATE=utf8_bin

测试数据为:

INSERT INTO `tmysql_test_lianxu_3` VALUES (1, 1, '2014-1-1 21:00:00', '2014-1-2 07:00:00');

INSERT INTO `tmysql_test_lianxu_3` VALUES (2, 1, '2014-1-2 15:37:57', '2014-1-2 21:00:00');

INSERT INTO `tmysql_test_lianxu_3` VALUES (3, 2, '2014-1-1 09:00:00', '2014-1-1 15:00:00');

INSERT INTO `tmysql_test_lianxu_3` VALUES (4, 2, '2014-1-2 09:00:00', '2014-2-1 16:00:00');

INSERT INTO `tmysql_test_lianxu_3` VALUES (5, 1, '2014-1-4 10:00:00', '2014-1-4 18:00:00');

INSERT INTO `tmysql_test_lianxu_3` VALUES (6, 1, '2014-1-5 12:00:00', '2014-1-5 13:00:00');

INSERT INTO `tmysql_test_lianxu_3` VALUES (7, 2, '2014-1-10 00:00:00', '2014-1-10 06:00:00');

INSERT INTO `tmysql_test_lianxu_3` VALUES (8, 2, '2014-1-11 13:00:00', '2014-1-11 18:00:00');

INSERT INTO `tmysql_test_lianxu_3` VALUES (10, 2, '2014-1-12 12:00:00', '2014-1-12 18:00:00');

INSERT INTO `tmysql_test_lianxu_3` VALUES (11, 1, '2014-1-8 06:00:00', '2014-1-8 16:00:00');

INSERT INTO `tmysql_test_lianxu_3` VALUES (12, 2, '2014-1-11 21:00:00', '2014-1-12 06:00:00');

在Oracle中可以使用row_number搞定,mysql中怎么做呢?

可以参考链接:

首先看原文中给出的答案:

SELECT uid, days, COUNT(*) AS num

FROM (SELECT uid,

@cont_day :=

(CASE

WHEN (@last_uid = uid AND DATEDIFF(login_dt, @last_dt) = 1) THEN

(@cont_day + 1)

ELSE

1

END) AS days,

(@cont_ix := (@cont_ix + IF(@cont_day = 1, 1, 0))) AS cont_ix,

@last_uid := uid,

@last_dt := login_dt

FROM (SELECT uid, DATE(sts) AS login_dt

FROM tmysql_test_lianxu_3

ORDER BY uid, sts) AS t,

(SELECT @last_uid := '',

@last_dt := '',

@cont_ix := 0,

@cont_day := 0) AS t1) AS t2

GROUP BY uid, days;

也是使用了mysql模拟oracle的row_number函数。

运行结果是:

eba541d85e2881278ddf945397755742.png

我看了半天发现结果好像不是我想要的,我想要的是要有开始时间,结束时间之类的。

看下中间表再说:

SELECT uid,

@cont_day :=

(CASE

WHEN (@last_uid = uid AND DATEDIFF(login_dt, @last_dt)=1) THEN

(@cont_day + 1)

ELSE

1

END) AS days,

(@cont_ix := (@cont_ix + IF(@cont_day = 1, 1, 0))) AS cont_ix,

@last_uid := uid,

@last_dt := login_dt login_day

FROM (SELECT uid, DATE(sts) AS login_dt

FROM tmysql_test_lianxu_3

ORDER BY uid, sts) AS t,

(SELECT @last_uid := '',

@last_dt := '',

@cont_ix := 0,

@cont_day := 0) AS t1

结果为:

4e1a2400aa721080db56bd019faccfc6.png

看了下可以这么做,连续日期取最大的days,开始时间,结束时间去login_day,而是这样写了:

SELECT uid, max(days) lianxu_days, min(login_day) start_date,max(login_day) end_date

FROM (SELECT uid,

@cont_day :=

(CASE

WHEN (@last_uid = uid AND DATEDIFF(login_dt, @last_dt)=1) THEN

(@cont_day + 1)

ELSE

1

END) AS days,

(@cont_ix := (@cont_ix + IF(@cont_day = 1, 1, 0))) AS cont_ix,

@last_uid := uid,

@last_dt := login_dt login_day

FROM (SELECT uid, DATE(sts) AS login_dt

FROM tmysql_test_lianxu_3

ORDER BY uid, sts) AS t,

(SELECT @last_uid := '',

@last_dt := '',

@cont_ix := 0,

@cont_day := 0) AS t1) AS t2

GROUP BY uid, cont_ix;

结果是:

5529fc463457c99a8d24e29f77d43c6c.png

这里存在的问题是:表里面的的sts登录时间不能有2条uid相同时间在同一天内。

解决方法是:在case中添加一个<1 的判断条件

SELECT uid, max(days) lianxu_days, min(login_day) start_date,max(login_day) end_date

FROM (SELECT uid,

@cont_day :=

(CASE

WHEN (@last_uid = uid AND DATEDIFF(login_dt, @last_dt)=1) THEN

(@cont_day + 1)

WHEN (@last_uid = uid AND DATEDIFF(login_dt, @last_dt)<1) THEN

(@cont_day + 0)

ELSE

1

END) AS days,

(@cont_ix := (@cont_ix + IF(@cont_day = 1, 1, 0))) AS cont_ix,

@last_uid := uid,

@last_dt := login_dt login_day

FROM (SELECT uid, DATE(sts) AS login_dt

FROM tmysql_test_lianxu_3

ORDER BY uid, sts) AS t,

(SELECT @last_uid := '',

@last_dt := '',

@cont_ix := 0,

@cont_day := 0) AS t1) AS t2

GROUP BY uid, cont_ix;

存在的问题:

时间sts的时分秒不见了。

--------------------------------------------Oracle可以这样做-------------------------------------------------

create table TSQL_TEST_LIANXU_4

(

ID NUMBER(4) not null,

U_ID NUMBER(4),

STS TIMESTAMP(6),

ETS TIMESTAMP(6)

);

insert into TSQL_TEST_LIANXU_4 (ID, U_ID, STS, ETS)

values (1, 1, to_timestamp('01-01-2014 21:00:00.000000', 'dd-mm-yyyy hh24:mi:ss.ff'), to_timestamp('02-01-2014 07:00:00.000000', 'dd-mm-yyyy hh24:mi:ss.ff'));

insert into TSQL_TEST_LIANXU_4 (ID, U_ID, STS, ETS)

values (2, 1, to_timestamp('02-01-2014 15:37:57.000000', 'dd-mm-yyyy hh24:mi:ss.ff'), to_timestamp('02-01-2014 21:00:00.000000', 'dd-mm-yyyy hh24:mi:ss.ff'));

insert into TSQL_TEST_LIANXU_4 (ID, U_ID, STS, ETS)

values (3, 2, to_timestamp('01-01-2014 09:00:00.000000', 'dd-mm-yyyy hh24:mi:ss.ff'), to_timestamp('01-01-2014 15:00:00.000000', 'dd-mm-yyyy hh24:mi:ss.ff'));

insert into TSQL_TEST_LIANXU_4 (ID, U_ID, STS, ETS)

values (4, 2, to_timestamp('02-01-2014 09:00:00.000000', 'dd-mm-yyyy hh24:mi:ss.ff'), to_timestamp('01-02-2014 16:00:00.000000', 'dd-mm-yyyy hh24:mi:ss.ff'));

insert into TSQL_TEST_LIANXU_4 (ID, U_ID, STS, ETS)

values (5, 1, to_timestamp('04-01-2014 10:00:00.000000', 'dd-mm-yyyy hh24:mi:ss.ff'), to_timestamp('04-01-2014 18:00:00.000000', 'dd-mm-yyyy hh24:mi:ss.ff'));

insert into TSQL_TEST_LIANXU_4 (ID, U_ID, STS, ETS)

values (6, 1, to_timestamp('05-01-2014 12:00:00.000000', 'dd-mm-yyyy hh24:mi:ss.ff'), to_timestamp('05-01-2014 13:00:00.000000', 'dd-mm-yyyy hh24:mi:ss.ff'));

insert into TSQL_TEST_LIANXU_4 (ID, U_ID, STS, ETS)

values (7, 2, to_timestamp('10-01-2014 00:00:00.000000', 'dd-mm-yyyy hh24:mi:ss.ff'), to_timestamp('10-01-2014 06:00:00.000000', 'dd-mm-yyyy hh24:mi:ss.ff'));

insert into TSQL_TEST_LIANXU_4 (ID, U_ID, STS, ETS)

values (8, 2, to_timestamp('11-01-2014 13:00:00.000000', 'dd-mm-yyyy hh24:mi:ss.ff'), to_timestamp('11-01-2014 18:00:00.000000', 'dd-mm-yyyy hh24:mi:ss.ff'));

insert into TSQL_TEST_LIANXU_4 (ID, U_ID, STS, ETS)

values (10, 2, to_timestamp('12-01-2014 12:00:00.000000', 'dd-mm-yyyy hh24:mi:ss.ff'), to_timestamp('12-01-2014 18:00:00.000000', 'dd-mm-yyyy hh24:mi:ss.ff'));

insert into TSQL_TEST_LIANXU_4 (ID, U_ID, STS, ETS)

values (11, 1, to_timestamp('08-01-2014 06:00:00.000000', 'dd-mm-yyyy hh24:mi:ss.ff'), to_timestamp('08-01-2014 16:00:00.000000', 'dd-mm-yyyy hh24:mi:ss.ff'));

本来想使用row_number的,结果没搞定。

select t.u_id,

to_char(MIN(t.sts), 'yyyy-mm-dd') start_date,

to_char(MAX(t.sts), 'yyyy-mm-dd') end_date,

trunc(MAX(t.sts)) - trunc(MIN(t.sts)) + 1

from (select z.u_id, z.sts, trunc(z.sts) - trunc(z.min_days) - rownum rn

from (select (select min(sts) from tsql_test_lianxu_4) min_days,

t.*

FROM tsql_test_lianxu_4 t

order by u_id, sts) z) t

group by u_id, rn

order by 1, 2

Oracle中这样查询是有问题的,就是uid相同sts在同一天的记录不能有2条,

Oracle另一种方法:

SELECT u_id, MIN(sts) AS STARTDATE, MAX(sts), COUNT(u_id) AS ENDNUM

FROM (SELECT A.u_id,

to_date(to_char(A.sts, 'yyyy-mm-dd'), 'yyyy-mm-dd') sts,

to_date(to_char(A.sts, 'yyyy-mm-dd'), 'yyyy-mm-dd') - ROWNUM AS GNUM

FROM (SELECT *

FROM tsql_test_lianxu_4

ORDER BY u_id,sts) A)

GROUP BY u_id, GNUM

ORDER BY u_id, MIN(sts)

缺点:uid相同sts在同一天的记录不能有2条

欢迎各位留下更好的查询SQL,如本文中的SQL有问题也请指出,谢谢。

全文完。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值