【问题标题】:How to find the start_time and end_time range if found in the range then check start_date and end_date如果在范围内找到,如何找到 start_time 和 end_time 范围,然后检查 start_date 和 end_date
【发布时间】: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-042019-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-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 然后我得到所有与场地 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


【解决方案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')

【讨论】:

  • 感谢您的回答。给我一些时间检查
  • 更新了我的答案,再试一次告诉我还是不行
  • 让我再试一次。
  • 2019-03-01 是结束日期,13:00:00 是结束时间...对吗?
  • 没错,它会检查 2019-02-04 10:00 和 2019-01-03 13:00 之间是否有任何预订
猜你喜欢
  • 1970-01-01
  • 2023-03-12
  • 1970-01-01
  • 2022-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多