How to check the id is booked of for specific days?

徘徊边缘 提交于 2019-12-11 17:57:06

问题


I have a table and records are like below table.

id | list_id |venue_id |name | start_date          | end_date           |start_time |end_time | days 
1  |       1 |  1      |asdf |  2019-02-02 14:05:54| 2019-02-28 14:05:54|05:30      |10:00    | 1,2,3
2  |       7 |  2      |awed |  2019-02-10 15:02:24| 2019-02-20 15:02:24|07:30      |14:00    | 2,5
3  |       7 |  1      |mjgd |  2019-02-04 09:05:54| 2019-02-13 09:05:54|09:30      |18:00    | 4

Now What I am doing is I have to check the start_time and end_time range if found in the range then check start_date and end_date are in the range if found then display the records.

So below query is working for the above scenario

SELECT * FROM batch_list 
WHERE venue_id=1 AND (start_date <= '2019-03-01') AND (start_time <= '13:00:00') AND (end_date >= '2019-02-04') AND (end_time >= '10:00:00')";

Now I have one more column which is days. So what I am doing is venue_id=1 is booked for a day which is 1,2,3,4 then display the records.

So how do i check the days which is already booked for id 1?

So what query I have to the user it to check the days are already in the table or not of the venue id=1?

function fetchBatches($venue_id,$new_batch_start_date,$new_batch_end_date,$new_batch_start_time,$new_batch_end_time,$days)
    {
$where="venue_id=$venue_id AND (start_date <= '$new_batch_end_date') AND (start_time <= '$new_batch_end_time') AND (end_date >= '$new_batch_start_date') AND (end_time >= '$new_batch_start_time')";

        $result =$this->db->select('*')    
                    ->from('batch_list')
                    ->where($where)
                    ->get()
                    ->result();
            if ($result) {
                 return $result;
            }
            else{
                 return 0;
            }

    }

Would you help me out on this issue?


回答1:


A query to check whether one range overlaps another might look like this:

SELECT x.*
  FROM my_table x
 WHERE x.start_datetime < :end_datetime
   AND x.end\datetime >= :start_datetime;

But really your question is broader than this



来源:https://stackoverflow.com/questions/54721274/how-to-check-the-id-is-booked-of-for-specific-days

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!