简体   繁体   English

MYSQL-如何在日期范围内查找记录

[英]MYSQL - How to find records within a date range

The table I'm looking at has columns with fixed dates - EnteredDateTime and AuditDateTime . 我正在查看的表具有固定日期的列EnteredDateTimeAuditDateTime I need some help finding records that are older than 1 day and less than 7 days old 我需要一些帮助来查找早于1天且少于7天的记录

The code I'm using at the moment is - 我目前使用的代码是-

EnteredDateTime BETWEEN DATE_ADD(AuditDateTime, INTERVAL 1 day) AND DATE_ADD(AuditDateTime, INTERVAL 7 day)

And what is the problem with your current query? 您当前的查询有什么问题? Do you want older than 1 day and less than 7 days from today's date? 您是否要从今天起older than 1 day and less than 7 days If so, use DATE_SUB and NOW() : 如果是这样,请使用DATE_SUBNOW()

EnteredDateTime BETWEEN DATE_SUB(NOW(), INTERVAL 1 day)
                    AND DATE_SUB(NOW(), INTERVAL 7 day)

Or if one of the columns answer this condition? 或者,如果其中一列满足此条件?

EnteredDateTime BETWEEN DATE_SUB(NOW(), INTERVAL 1 day)
                    AND DATE_SUB(NOW(), INTERVAL 7 day)
OR  AuditDateTime BETWEEN DATE_SUB(NOW(), INTERVAL 1 day)
                      AND DATE_SUB(NOW(), INTERVAL 7 day)

You can use GREATEST() or LEAST() if you know which one of the dates you want to check against (lowest or highest) to avoid the use of OR . 如果知道要检查哪个日期(最低或最高)以避免使用OR可以使用GREATEST()LEAST()

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

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