0

我有几位顾问记录他们的工作开始/结束时间。因为他们有两个小时的最低费用,我需要确保我正确计算了他们的工作时间。当他们在同一站点上有连续的作业时,就会出现问题,作业之间的空闲时间介于 15 到 60 分钟之间。

下面是我的样表按cid、job_date、start_time排序的查询结果。对于此特定示例,所有作业时间条目均针对同一天:

 id  job_num    cid job_date    start_time           end_time
 19  130513-20  6   05/13/2013  2013-05-13 13:30:00  2013-05-13 16:30:00
 10  130513-10  7   05/13/2013  2013-05-13 11:00:00  2013-05-13 13:00:00
 16  130513-17  13  05/13/2013  2013-05-13 14:45:00  2013-05-13 15:30:00
 15  130513-16  16  05/13/2013  2013-05-13 14:45:00  2013-05-13 16:15:00
 18  130513-19  34  05/13/2013  2013-05-13 11:15:00  2013-05-13 12:15:00
 8   130513-08  44  05/13/2013  2013-05-13 12:00:00  2013-05-13 14:30:00
 6   130513-06  47  05/13/2013  2013-05-13 11:00:00  2013-05-13 12:00:00
 9   130513-09  47  05/13/2013  2013-05-13 12:00:00  2013-05-13 12:30:00
 14  130513-15  47  05/13/2013  2013-05-13 14:30:00  2013-05-13 15:15:00
 3   130513-03  50  05/13/2013  2013-05-13 09:00:00  2013-05-13 10:45:00
 7   130513-07  50  05/13/2013  2013-05-13 11:00:00  2013-05-13 12:00:00
 11  130513-11  50  05/13/2013  2013-05-13 13:00:00  2013-05-13 14:30:00
 17  130513-18  50  05/13/2013  2013-05-13 15:00:00  2013-05-13 16:15:00
 2   130513-02  52  05/13/2013  2013-05-13 09:30:00  2013-05-13 10:30:00
 4   130513-04  52  05/13/2013  2013-05-13 10:30:00  2013-05-13 11:00:00
 5   130513-05  52  05/13/2013  2013-05-13 11:00:00  2013-05-13 13:00:00
 12  130513-12  52  05/13/2013  2013-05-13 14:00:00  2013-05-13 14:45:00
 13  130513-13  52  05/13/2013  2013-05-13 15:00:00  2013-05-13 17:00:00
 20  130513-21  61  05/13/2013  2013-05-13 11:00:00  2013-05-13 12:00:00
 1   130513-01  70  05/13/2013  2013-05-13 14:30:00  2013-05-13 15:15:00

我要完成的是更新特定行(job_num)的end_time,使其等于同一顾问(cid)查询中下一个job_num的end_time,但前提是差异(空闲时间)在第一个作业的 end_time 和下一个作业的 start_time <= 30 分钟之间。基本上,我想将空闲时间添加到比较中第一个作业的 end_time 中。

例如:

 id  job_num    cid job_date    start                end                
 3   130513-03  50  05/13/2013  2013-05-13 09:00:00  2013-05-13 10:45:00  <-- original
 7   130513-07  50  05/13/2013  2013-05-13 11:00:00  2013-05-13 12:00:00 
 11  130513-11  50  05/13/2013  2013-05-13 13:00:00  2013-05-13 14:30:00  <-- original
 17  130513-18  50  05/13/2013  2013-05-13 15:00:00  2013-05-13 16:15:00 
...
 12  130513-12  52  05/13/2013  2013-05-13 14:00:00  2013-05-13 14:45:00  <-- original
 13  130513-13  52  05/13/2013  2013-05-13 15:00:00  2013-05-13 17:00:00 

变得:

 3   130513-03  50  05/13/2013  2013-05-13 09:00:00  2013-05-13 11:00:00  <-- changed
 7   130513-07  50  05/13/2013  2013-05-13 11:00:00  2013-05-13 12:00:00 
 11  130513-11  50  05/13/2013  2013-05-13 13:00:00  2013-05-13 15:00:00  <-- changed
 17  130513-18  50  05/13/2013  2013-05-13 15:00:00  2013-05-13 16:15:00 
...
 12  130513-12  52  05/13/2013  2013-05-13 14:00:00  2013-05-13 15:00:00  <-- changed
 13  130513-13  52  05/13/2013  2013-05-13 15:00:00  2013-05-13 17:00:00 

我将如何进行此更改?我已经看到使用临时表和变量来解决类似问题,这些临时表和变量非常适合检查时差,但没有一个可以适应我的特定问题(以我对 MySQL 查询的有限知识)。我也将此问题标记为 PHP,因为在 PHP 中执行此操作可能会更简单。

任何帮助将不胜感激!

4

2 回答 2

0

我假设那idPrimary Key桌子的,它是AUTO-INCREMENTing。

我有这个UPDATE查询:

UPDATE table T1
    SET T1.end_time = (
            SELECT T2.end_time
                FROM table T2
                WHERE T2.cid = T1.cid
                    AND T2.job_date = T1.job_date
                    AND T2.id > T1.id
                    AND T2.start_time <= DATE_ADD(T1.end_time, INTERVAL 30 MINUTE)
                ORDER BY T2.id ASC
                LIMIT 1
        )
    WHERE id IN (
        SELECT DISTINCT T1.Id
            FROM table T1
            INNER JOIN table T2 ON T2.cid = T1.cid
                AND T2.job_date = T1.job_date
                AND T2.id > T1.id
                AND T2.start_time <= DATE_ADD(T1.end_time, INTERVAL 30 MINUTE)
            WHERE T1.job_date = @date
        );

虽然它有点复杂,而且效率不高。但由于这只是一个查询,我认为您需要每次更新end_time(每天/每周/等)。

并确保您设置了@date变量的值,或者您可以将其替换为您想要的日期。

我认为使用CURSOR(这很慢)将比我上面的查询更有效。

这是一个示例CURSOR查询:

DECLARE id INT(11);
DECLARE newEndDate DATETIME;

DECLARE noMoreRows BIT(1);
DECLARE customCursor CURSOR FOR
    SELECT T1.Id
        , CAST(SUBSTRING_INDEX(GROUP_CONCAT(T2.start_date ORDER BY T2.Id ASC), ',', 1) AS DATETIME) AS NewEndDate
        FROM table T1
        INNER JOIN table T2 ON T2.cid = T1.cid
             AND T2.job_date = T1.job_date
             AND T2.id > T1.id
             AND T2.start_time <= DATE_ADD(T1.end_time, INTERVAL 30 MINUTE)
        WHERE T1.job_date = @date
        GROUP BY T1.Id
        HAVING NewEndDate <> '' OR NewEndDate IS NOT NULL;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET noMoreRows = 1;

OPEN customCursor;
customLoop: LOOP

    FETCH customCursor INTO id, newEndDate;

    IF noMoreRows THEN
      LEAVE customLoop;
   END IF;

    UPDATE table
        SET end_date = newEndDate
        WHERE id = id;

END LOOP customLoop;
CLOSE customCursor;

请注意,它CURSOR应该在一个STORED PROCEDURE. 这是的链接CURSORS

于 2013-06-05T05:39:59.393 回答
0

我假设您正在使用 php 格式化开始时间和结束时间,然后插入到数据库表中。

$start = date("Y-m-d H:i:s",time());
$query = "select * from table name where cid = $cid and  UNIX_TIMESTAMP($start) - UNIX_TIMESTAMP(end_time) <= 1800";
mysqli_query($con,$query);

if ($result=mysqli_query($con,$query))
  {
    $rowcount=mysqli_num_rows($result);

    if($rowcount > 1)
    { 
     $row = mysqli_fetch_array($result, MYSQLI_ASSOC);

     query2 = "update tablename set end_time = '$start' where id = '$row['id']'";

     query3 = "insert into tablename(cid,date,start_time,end_time) values($cid,$date,$start,$end)";


    }else{

    //do the insert as such
    query2 = "insert into tablename(cid,date,start_time,end_time) values($cid,$date,$start,$end)";

    }

    mysqli_free_result($result);
  }
于 2013-06-05T06:51:21.023 回答