我有两个约会。 2019-01-01(fromDate)
和2019-01-10(toDate)
。现在,我只想搜索每个日期的13:00
至16:00
时间。请问是否可以仅使用查询?任何答案都很感激
SELECT *
FROM table
WHERE fromDate >= 2019-01-01 AND toDate <= 2019-01-10
答案 0 :(得分:5)
您可以尝试将STR_TO_DATE函数与格式和HOUR
函数一起使用。
SELECT *
FROM table
WHERE
fromDate >= STR_TO_DATE('2019-01-01', '%Y-%m-%d')
AND
toDate <= STR_TO_DATE('2019-01-10', '%Y-%m-%d')
AND
(HOUR(fromDate) BETWEEN 13 AND 16 OR HOUR(toDate) BETWEEN 13 AND 16)
答案 1 :(得分:3)
在日期和时间上使用单独的条件:
SELECT *
FROM table
WHERE fromDate >= '2019-01-01' AND
toDate < '2019-01-11' and
time(fromdate) between '13:00:00' and '16:00:00' and
time(todate) between '13:00:00' and '16:00:00;
或者,如果您不想完全使用16:00:00
,则可以使用hour()
:
SELECT *
FROM table
WHERE fromDate >= '2019-01-01' AND
toDate < '2019-01-11' and
hour(fromdate) in (13, 14, 15) and
hour(todate) in (13, 14, 15)