我在数据库中见下表。
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
现在,我必须找到venue_id=1
和2019-02-04
的范围之间的起始日期和2019-03-01
的END_DATE。所以我用下面的查询。
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
然后我得到了所有与相关记录到venue_id 1,但如果我更改时间10:00
到13:00
然后,我没有得到记录。
我需要找出是否有任何START_TIME和END_TIME是与否的范围内使用。如果发现再检查起始日期日期和结束日期。
你会帮我在这个问题?
模型
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;
}
}
正如一些人所说,这是更好的日期和时间结合到数据库中。
对于你的问题,我认为你要查询的是这个:
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')