doudu9148 2014-02-05 19:44
浏览 14

保留没有任何时间重叠

I am writing php script which checks and allocates available free slot to reserve a certain machine. The table looks like this:

    machine      start_time    duration(mins)
      1             11:00       30 
      1             12:00       40
      1             13:00       30
      1             14:00       40
      2             11:00       10
      2             12:10       30
      2             13:00       10

when the user gives start time the system should automatically assign machine and max possible duration the user can use. No partial nor total overlap on the same machine.

Here is part of the script I tried:

    $time = $_POST['starttime'];
    $duration = $_POST['duration'];

    $query1 = "SELECT machine_id FROM machinetable";
    $res1 = mysql_query($query1$query1,$conn);

    while($row1 = mysql_fetch_array($res1)) {
        $y = $row1['machine_id'];
        $qry = "SELECT * FROM tableabove where machine_id = '$y' Order By start_time DESC";

        $result = mysql_query($qry,$conn);
        while($row = mysql_fetch_array($result)) {
         // WHAT SHALL I ADD HERE.....JUST SOME HINT OR ANOTHER MEANS THAT HELPS
        }
    }
  • 写回答

1条回答 默认 最新

  • doujiao3072 2014-02-05 19:54
    关注

    Is this what you are looking for?

    select machine_id, duration
    from table t
    where starttime = USERSTARTTIME
    order by duration desc
    limit 1;
    

    This returns the machine and duration. Your php code can then do the "reservation".

    EDIT:

    Try this instead:

    select machine_id, duration - timediff(USERSTARTTIME, starttime) as durationForUser
    from table t
    where timediff(USERSTARTTIME, starttime) between 0 and duration
    order by duration - timediff(USERSTARTTIME, starttime) desc
    limit 1;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集