【发布时间】:2019-02-01 18:33:31
【问题描述】:
我在数据库中有一个下表。
id | list_id |venue_id |name | start_date | end_date |start_time | end_time
1 | 1 | 1 |asdf | 2019-02-02| 2019-02-28 |05:30 |10:00
2 | 7 | 2 |awed | 2019-02-10| 2019-02-20 |07:30 |14:00
3 | 7 | 1 |mjgd | 2019-02-04| 2019-02-13 |09:30 |18:00
现在,我必须在2019-02-04 和2019-03-01 的范围内找到venue_id=1 的开始日期和结束日期。所以我使用了下面的查询。
SELECT * FROM `batch_list` WHERE `venue_id` = 1 and ((start_date between '2019-02-04' and '2019-03-01') OR (end_date between '2019-02-04' and '2019-03-01'))
现在我必须找到venue_id=1 的日期和时间,它的范围是2019-02-04 和2019-03-01 和05:00 到18:00。所以我尝试了下面的查询
SELECT * FROM `batch_list` WHERE `venue_id` = 1 and ((start_date between '2019-02-04' and '2019-03-01') OR (end_date between '2019-02-04' and '2019-03-01')) and((start_time <= `end_time`) and (`start_time` between '05:00' and '18:00')and (end_time between '05:00' and '18:00'))
所以到目前为止没有问题。
我有一个时间是 05:00 到 20:00 然后我得到所有与场地 ID 1 相关的记录但是如果我将时间 10:00 更改为 13:00 那么我没有得到记录.
我需要找出该范围内是否有可用的 start_time 和 end_time。如果找到,请检查 start_date 和 end_date。
你能帮我解决这个问题吗?
型号
function fetchBatches($venue_id,$new_batch_start_date,$new_batch_end_date,$new_batch_start_time,$new_batch_end_time)
{
$where="batch_venue_id='$venue_id' and ((start_date between '$new_batch_start_date' and '$new_batch_end_date')OR (end_date between '$new_batch_start_date' and '$new_batch_end_date')) and ((start_time<=end_time) and (start_time < '$new_batch_start_time' and start_time < '$new_batch_end_time'))";
$result =$this->db->select('*')
->from('batch_list')
->where($where)
->get()
->result();
if ($result) {
return $result;
}
else{
return 0;
}
}
【问题讨论】:
-
10:00 到 01:00 =
between 10 and 24 or 0 and 1? -
哦!我想念那个。等等,我正在更新问题。那是 10:00 到 13:00。更新的问题。
-
您可以通过将日期和时间存储为单个实体来为自己省去一个痛苦的世界
-
我不明白 - 你真的想找到两个时间间隔的重叠吗?
-
@splash58,我的表中已有记录。如果管理员想再次创建 place_id =1 记录,那么首先它会检查记录中是否有 start_time 和 end_time 如果记录可用,那么它将检查日期
标签: php mysql codeigniter-3