我正在编写php脚本,它检查并分配可用的空闲插槽以保留某台机器。该表如下所示:
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
当用户给出开始时间时,系统应自动分配机器和用户可以使用的最大可能持续时间。同一台机器上没有部分重叠或完全重叠。
以下是我尝试的脚本的一部分:
$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
}
}
答案 0 :(得分:0)
这是你在找什么?
select machine_id, duration
from table t
where starttime = USERSTARTTIME
order by duration desc
limit 1;
返回机器和持续时间。然后你的PHP代码可以进行“预订”。
编辑:
请改为尝试:
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;