0

需要一个 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
4

1 回答 1

0

所以我能够创建一个解决方案……ish。请看一下并找到一种可以优化的方法!

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

于 2013-11-01T01:42:23.093 回答