简体   繁体   English

MYSQL现在选择日期 - 1天时间和日期之间

[英]MYSQL select date now-1 day with TIME and between date

I want to select all rows between current day-1 with specific time 08:00am and to date now also with specific time.我想选择当前第 1 天与特定时间上午 08:00 之间的所有行,现在也选择特定时间。 This is not working for example sample line code这不适用于例如示例行代码

WHERE SOME_DATE_COLUMN BETWEEN DATE_SUB(DATE(NOW() - INTERVAL 1 DAY) '08:00:00', '%Y-%m-%d %H:%i:%s') AND DATE_SUB(DATE(NOW()) '08:00:00', '%Y-%m-%d %H:%i:%s');

I tried also DATE_FORMAT我也试过 DATE_FORMAT

WHERE SOME_DATE_COLUMN BETWEEN DATE_FORMAT(DATE(NOW() - INTERVAL 1 DAY) '08:00:00', '%Y-%m-%d %H:%i:%s') AND DATE_FORMAT(DATE(NOW()) '08:00:00', '%Y-%m-%d %H:%i:%s');

If I run sql today I want to select all record between current date minus 1 day from 08:00am to current date 08:00am如果我今天运行 sql,我想选择从 08:00am 到当前日期 08:00am 的当前日期减去 1 天之间的所有记录

I would do:我会做:

where 
    some_date_column >= current_date - interval 16 hour
    and some_date_column < current_date + interval 8 hour

current_date gives you the current date (without the time part). current_date为您提供当前日期(不含时间部分)。 You can then add and substract the required number of hours.然后,您可以添加和减去所需的小时数。

Note that this query does not use between , but instead half-open intervals.请注意,此查询不使用between ,而是使用半开区间。 If you want consistent, adjacent segments in time, then you want to exclude one of the bounds (by convention, generally the outer bound is excluded).如果您希望在时间上保持一致的相邻段,那么您希望排除其中一个边界(按照惯例,通常排除外部边界)。

Demo on DB Fiddle : DB Fiddle 上的演示

select current_date - interval 16 hour, current_date + interval 8 hour
current_date - interval 16 hour | current_date + interval 8 hour
:------------------------------ | :-----------------------------
2020-02-04 08:00:00             | 2020-02-05 08:00:00

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM