【问题标题】:How to find the records which is the range of two dates and time如何查找两个日期和时间范围内的记录
【发布时间】:2018-08-19 04:42:45
【问题描述】:

我有表和记录是这样的

id | list_id |venue_id |name | start_date | end_date |start_time | end_time 
1  |1        | 4       |xyz  |2018-08-20  |2018-09-02|5:00 AM     |11:00 AM
2  |2        | 3       |oig  |2018-08-20  |2018-08-30|9:00 AM     |12:00 PM
3  |1        | 4       |sad  |2018-08-30  |2018-09-10|11:30 AM    |3:00 PM
4  |1        | 4       |sfd  |2018-08-28  |2018-09-18|9:00 AM     |1:00 PM
5  |1        | 4       |khg  |2018-08-29  |2018-09-10|6:00 PM     |11:00PM

现在,我必须在'2018-08-18''2018-09-28' 的范围内找到 start_date 和 end_date。所以我使用了下面的查询

SELECT * FROM `list` WHERE `venue_id` = '4' and ((start_date between '2018-08-18' and '2018-09-28')OR (end_date between '2018-08-18' and '2018-09-28'))

所以我得到了一个正确的输出。

id | list_id |venue_id |name | start_date | end_date |start_time | end_time 
1  |1        | 4       |xyz  |2018-08-20  |2018-09-02|5:00 AM     |11:00 AM
3  |1        | 4       |sad  |2018-08-30  |2018-09-10|11:30 AM    |3:00 PM
4  |1        | 4       |sfd  |2018-08-28  |2018-09-18|9:00 AM     |1:00 PM
5  |1        | 4       |khg  |2018-08-29  |2018-09-10|6:00 PM     |11:00PM

现在我必须找到 place_id=4 的日期和时间,范围为 '2018-08-18''2018-09-28'"5:00AM""5:00PM" 所以我尝试查询,但它只显示前两条记录。我认为应该显示三个记录。对吧?

SELECT * FROM `batch_list` WHERE `batch_venue_id` = '4' and ((start_date between '2018-08-18' and '2018-09-28')OR (end_date between '2018-08-18' and '2018-09-28')) and ((start_time<=end_time and "5:00 AM">=start_time and "5:00 PM" <= end_time)) OR ((end_time<=start_time and "5:00 AM"<=end_time or "5:00 PM" >= start_time)) 

我的预期输出是

id | list_id |venue_id |name | start_date | end_date |start_time | end_time 
1  |1        | 4       |xyz  |2018-08-20  |2018-09-02|5:00 AM     |11:00 AM
3  |1        | 4       |sad  |2018-08-30  |2018-09-10|11:30 AM    |3:00 PM
4  |1        | 4       |sfd  |2018-08-28  |2018-09-18|9:00 AM     |1:00 PM

【问题讨论】:

  • 第 1 步:使用适当的日期时间数据类型将日期和时间存储为单个实体

标签: mysql


【解决方案1】:

我认为当您创建表时,您将 end_timestart_time 列声明为 varchar 类型,而不是 time 类型。所以,它会比较字符串。例如,“9:10 AM”>“9:00 PM”(“1”大于“0”)。

我创建了一个表格并插入了您的数据。我得到了你预期的结果。

CREATE TABLE SampleTable( id int auto_increment not null,
                        list_id int,
                        venue_id int,
                        item_name varchar(3),
                        start_date date,
                        end_date date,
                        start_time time,
                        end_time time,
                        primary key(id));

INSERT INTO SampleTable( list_id, venue_id, item_name, start_date, end_date, start_time,end_time)
VALUES
    (1, 4,'xyz', '2018-08-20','2018-09-02','5:00','11:00'),
    (2, 3, 'oig','2018-08-20', '2018-08-30', '9:00', '12:00'),
    (1, 4, 'sad','2018-08-30', '2018-09-10', '11:30', '15:00'),
    (1, 4, 'sfd','2018-08-28', '2018-09-18', '9:00', '13:00'),
    (1,4, 'khg', '2018-08-29', '2018-09-10', '18:00','11:00');

SELECT * FROM sampletable
WHERE venue_id = 4 
    AND ( (start_date between '2018-08-18' and '2018-09-28') or (end_date between '2018-08-18' and '2018-09-28') ) 
    AND ( (start_time<=end_time) and  (start_time between '5:00' and '17:00') and (end_time between '5:00' and '17:00') );     

【讨论】:

  • 感谢您的回复,给我一些时间来实现它。
  • 我正在插入时间 5:00 AM、1:00 PM、5:30 PM 等。
  • start_timeend_time 列的数据类型是什么?
  • 请分享不赞成票的原因。没有理由我们无法理解答案中的问题。
  • 正如我上面提到的,它会将字符串与字符串进行比较。在我的示例中,我已将 start_timeend_time 声明为 time 数据类型。
猜你喜欢
  • 2012-01-20
  • 2023-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-09
  • 2021-09-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多