![](/img/trans.png)
[英]How to query to return data range between start_date and end_date?
[英]How to find the start_time and end_time range if found in the range then check start_date and end_date
我在数据库中有一个下表。
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
的 start_date 和 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 可用。 如果找到,则检查 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;
}
}
正如一些人所建议的,最好将日期和时间合并到数据库中。
对于您的问题,我认为您想要的查询是这样的:
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')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.