0

我正在处理一个时间安排项目,我可以按时间安排材料,我真的很困惑如何在时间范围内跟踪时间冲突。我使用了许多(我认为)算法,但仍然没有运气。我的大脑已经饱和,这就是为什么我在这里问任何可以帮助我的人。

所以我的数据库中有这些数据:

    CREATE TABLE schedule_dummy
    (
    `materialID` int,
    `date_reserve` date,
    `start_time` time,
    `end_time` time
    );

   ------DB Table schedule_dummy --------
   material |        date     |   start_time  | end_time   
    ----------------------------------------------------------
    7       |    2013-08-01   |  13:00:00     | 14:00:00    
   10       |    2013-09-01   |  13:30:00     | 14:00:00    
    ---------------------------------------------------------- 

我使用这些数据来检查特定材料在特定时间的可用性。因此,如果数据库中的start_timeend_timeequal toin between那些时间的值,则该材料将不可用。我尝试了很多查询,但不适用于很多情况。
以下是我迄今为止尝试过的一些查询。

   $sql = "SELECT materialID
          FROM schedule_dummy WHERE (date_reserve = ?)
          AND (? BETWEEN start_time AND end_time
          OR ? BETWEEN start_time AND end_time)";

   //I've tried this query but in some cases doesn't work dunno if I'm missing something.
    $sql = "SELECT * FROM schedule_dummy
        WHERE date_reserve = ?
        AND start_time >=? AND start_time <=?";

这是我的 php 代码 注意:我正在使用 codeigniter:

   $start =  date("H:i:s", strtotime($this->input->post('start')));
    $end =  date("H:i:s", strtotime($this->input->post('end')));
    $date_reserve =  $this->input->post('date_reserve');
    $id =  $this->input->post('id');


    //$sql = "SELECT materialID FROM schedule_dummy WHERE (date_reserve = ?) AND (? BETWEEN start_time AND end_time OR ? BETWEEN start_time AND end_time)";
    $sql = "SELECT * FROM schedule_dummy WHERE date_reserve = ? AND start_time >=? AND end_time <=?";
    $query = $this->db->query($sql,array($date_reserve,$start,$end));

    $ids = array();
    if($query->num_rows() > 0 ){
        foreach($query->result() as $rows){
            $ids[] = $rows->materialID;
        }
    }

    $data = array();
    $n = count($ids);

    $idz = array();
        if($n > 0){
            $q = $this->db->select('*')->where_not_in('id',$ids)->where('cid', $id)->get('materials');

            if($q->num_rows() > 0){
                foreach($q->result() as $row){
                    $data[] = $row;
                }
            }

        }else{
            $q = $this->db->select('*')->where('cid',$id)->get('materials');

            if($q->num_rows() > 0 ){
                foreach($q->result() as $row){
                    $data[] = $row;
                }
            }

        }
        return $data;

那么任何知道一些算法来追踪时间冲突的人都可以吗?这样我就可以在我的数据库中检索那些可用的材料?谢谢!

4

2 回答 2

1

如果您想查看某个项目是否在某个日期和特定时间使用,以下应该可以为您解决问题:

select
    material
from
    schedule_dummy
where
    date_reserve=checkDate
    and start_time <= checkTime
    and end_time >= checkTime
于 2013-09-01T07:32:59.877 回答
0
$qry = "SELECT * FROM schedule WHERE day='$day' AND starttime>='$starttime' AND endtime<'$endtime'";
        $result = mysql_query($qry);
        if($result) {
            if(mysql_num_rows($result) > 0) {
                $errmsg_arr[] = 'The Room or Time Schedule is  already exist in the database';
                $errflag = true;
            }
            @mysql_free_result($result);
        }
        else {
            die("Query failed");
        }


    //If there are input validations, redirect back to the registration form
    if($errflag) {
        $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
        session_write_close();
        echo "<script>alert('The Room or Time Schedule is  already exist in the database.');history.back();</script>";
        exit();
    }
于 2017-01-17T07:27:23.673 回答