Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
相同用户和相同状态的连续实例-MYSQL_Mysql_Sql_Date - Fatal编程技术网

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

相同用户和相同状态的连续实例-MYSQL,mysql,sql,date,Mysql,Sql,Date,需要一个MYSQL查询,该查询将查找数据库中连续且具有相同用户id和状态id的所有事件。如何在避免循环的同时查找连续事件 我遇到的问题是,我不知道如何检查时间间隔。因此,如果有人在上午10:00到10:30之间有空,那么从上午11:00到下午12:30,如何防止系统只说上午10:00到下午12:30 我们正在使用PHP和Javascript创建日历,但希望有一个表视图,可以用于许多不同的查询 如果你有任何问题,请告诉我 CREATE TABLE `events` ( `event_id` i

需要一个MYSQL查询,该查询将查找数据库中连续且具有相同用户id和状态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

所以我能够创造出一个解决方案…伊什。请看一看,并找到一种方法,它可以优化

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状态MINTIMESTAMP1 TIMESTAMP2 MAXDURATION

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

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


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

您的数据会导致重复的主键错误。抱歉,我创建了一些假数据来提供一个好的样本集,但是忘记了更新事件id。现在已经修复了。