如何找到START_TIME和END_TIME范围是否在范围内发现的,然后检查起始日期日期和结束日期

问题描述 投票:1回答:1

我在数据库中见下表。

    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=12019-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-042019-03-0105:0018: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:0020:00然后我得到了所有与相关记录到venue_id 1,但如果我更改时间10:0013: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;
            }

    }
php mysql codeigniter-3
1个回答
1
投票

正如一些人所说,这是更好的日期和时间结合到数据库中。

对于你的问题,我认为你要查询的是这个:

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')
© www.soinside.com 2019 - 2024. All rights reserved.