相同用户和相同状态的连续实例 - MYSQL

时间:2013-10-29 21:31:05

标签: mysql sql date

需要一个MYSQL查询,它将查找我的数据库中所有连续且具有相同user_id和status_id的事件。如何在避免循环的同时找到连续事件?

我遇到的问题是我不知道如何在两次之间检查休息时间。因此,如果有人从上午10:00到上午10:30可用,那么再次从上午11:00到下午12:30,如何防止系统只是说上午10:00 - 下午12:30。

我们使用PHP和Javascript来创建日历,但希望有一个表格视图,我们可以用它来进行多种不同的查询。

如果您有任何疑问,请与我们联系。

CREATE TABLE `events` (
  `event_id` int(11) NOT NULL auto_increment COMMENT 'ID',
  `user_id` int(11) NOT NULL,
  `date` date NOT NULL,
  `time` time NOT NULL,
  `status_id` int(11) default NULL,
  PRIMARY KEY  (`event_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1568 ;


INSERT INTO `events` VALUES(1, 101, '2013-08-14', '23:00:00', 2);
INSERT INTO `events` VALUES(2, 101, '2013-08-14', '23:15:00', 2);
INSERT INTO `events` VALUES(3, 101, '2013-08-14', '23:30:00', 2);
INSERT INTO `events` VALUES(4, 101, '2013-08-14', '23:45:00', 2);
INSERT INTO `events` VALUES(5, 101, '2013-08-15', '00:00:00', 2);
INSERT INTO `events` VALUES(6, 101, '2013-08-15', '00:15:00', 1);
INSERT INTO `events` VALUES(7, 500, '2013-08-14', '23:45:00', 1);
INSERT INTO `events` VALUES(8, 500, '2013-08-15', '00:00:00', 1);
INSERT INTO `events` VALUES(9, 500, '2013-08-15', '00:15:00', 2);
INSERT INTO `events` VALUES(10, 500, '2013-08-15', '00:30:00', 2);
INSERT INTO `events` VALUES(11, 500, '2013-08-15', '00:45:00', 1);
INSERT INTO `events` VALUES(12, 101, '2013-08-15', '01:15:00', 1);
INSERT INTO `events` VALUES(13, 101, '2013-08-15', '01:30:00', 1);
INSERT INTO `events` VALUES(14, 101, '2013-08-15', '01:45:00', 1);
INSERT INTO `events` VALUES(15, 101, '2013-08-15', '02:00:00', 1);

期望的输出

row |user_id | date_start | time_start | date_end   | time_end | status_id | duration
1   |101     |'2013-08-14'| '23:00:00' |'2013-08-15'|'00:15:00'| 2         | 5
2   |101     |'2013-08-15'| '00:15:00' |'2013-08-15'|'00:30:00'| 1         | 1
3   |500     |'2013-08-14'| '23:45:00' |'2013-08-15'|'00:15:00'| 1         | 2
4   |500     |'2013-08-15'| '00:15:00' |'2013-08-15'|'00:45:00'| 2         | 2
5   |500     |'2013-08-15'| '00:45:00' |'2013-08-15'|'01:00:00'| 2         | 1
6   |101     |'2013-08-15'| '01:15:00' |'2013-08-15'|'02:15:00'| 1         | 4

1 个答案:

答案 0 :(得分:0)

所以我能够创建一个解决方案......是的。请查看并找到可以优化的方法!

SELECT user1, user2, status_id, min(Timestamp1), Timestamp2, max(duration) FROM (
    SELECT x.user1, x.user2, x.status_id, x.Timestamp1, x.Timestamp2, min(x.difference) as duration
    FROM (

    SELECT e1.user_id AS user1, e2.user_id AS user2, e1.status_id, CONCAT(e1.date, " ", e1.time) AS Timestamp1, CONCAT(e2.date, " ", e2.time)AS Timestamp2,
    CAST(TIMEDIFF(CONCAT(e2.date, " ", e2.time),CONCAT(e1.date, " ", e1.time)) AS SIGNED)  AS 'difference'
    FROM `events` e1, `events` e2
    WHERE e1.user_id = e2.user_id AND e1.status_id = e2.status_id
    AND CAST(TIMEDIFF(CONCAT(e2.date, " ", e2.time),CONCAT(e1.date, " ", e1.time)) AS SIGNED) >= 0
    AND CAST(TIMEDIFF(CONCAT(e2.date, " ", e2.time),CONCAT(e1.date, " ", e1.time)) AS SIGNED) < 240000
    ORDER BY e1.user_id, e1.status_id, Timestamp1, Timestamp2) x,
    (

    SELECT e1.user_id AS user1, e2.user_id AS user2, e1.status_id, CONCAT(e1.date, " ", e1.time) AS Timestamp1, CONCAT(e2.date, " ", e2.time)AS Timestamp2,
    CAST(TIMEDIFF(CONCAT(e2.date, " ", e2.time),CONCAT(e1.date, " ", e1.time)) AS SIGNED)  AS 'difference'
    FROM `events` e1, `events` e2
    WHERE e1.user_id = e2.user_id AND e1.status_id = e2.status_id
    AND CAST(TIMEDIFF(CONCAT(e2.date, " ", e2.time),CONCAT(e1.date, " ", e1.time)) AS SIGNED) >= 0
    ORDER BY e1.user_id, e1.status_id, Timestamp1, Timestamp2) y

    WHERE x.user1 = y.user1 AND x.user2 = y.user2 AND x.Timestamp1 = y.Timestamp1
    AND ADDTIME(x.Timestamp2, '00:15:00') NOT IN (
      SELECT CONCAT(e2.date, " ", e2.time)AS Timestamp2
      FROM `events` e1, `events` e2
      WHERE e1.user_id = e2.user_id AND e1.status_id = e2.status_id
      AND x.user1 = e1.user_id AND x.status_id = e1.status_id
      AND x.Timestamp1 = CONCAT(e1.date, " ", e1.time)
      AND CAST(TIMEDIFF(CONCAT(e2.date, " ", e2.time),CONCAT(e1.date, " ", e1.time)) AS SIGNED) >= 0)

    GROUP BY x.user1, x.user2, x.status_id, x.Timestamp1
    ) z
GROUP BY user1, user2, status_id, Timestamp2

以下是它提供的结果:

USER1 USER2 STATUS MIN(TIMESTAMP1)TIMESTAMP2 MAX(DURATION)

101 101 1 2013-08-15 00:15:00 2013-08-15 00:15:00 0

101 101 1 2013-08-15 01:15:00 2013-08-15 02:00:00 4500

101 101 2 2013-08-14 23:00:00 2013-08-15 00:00:00 10000

500 500 1 2013-08-14 23:45:00 2013-08-15 00:00:00 1500

500 500 1 2013-08-15 00:45:00 2013-08-15 00:45:00 0

500 500 2 2013-08-15 00:15:00 2013-08-15 00:30:00 1500