MySQL 如何从mysql表中获取前一天的记录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16372579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the previous day records from mysql table?
提问by user001
I am trying to fetch previous day records from table but I am not finding how to do it exactly. Need your help please..
我正在尝试从表中获取前一天的记录,但我没有找到确切的方法。需要你的帮助。。
Table: RECORD_DATA
表:RECORD_DATA
id creationDate
1 | 2013-05-03 04:03:35 |
2 | 2013-05-03 04:03:35 |
Now I need to get all the records that were created on 2013-05-03. Time can be anything. So my query should have LIKE operator.
现在我需要获取在 2013-05-03 创建的所有记录。时间可以是任何东西。所以我的查询应该有 LIKE 运算符。
I am using below query and it gives me empty set.
我正在使用下面的查询,它给了我空集。
select creationDate from RECORD_DATA
where creationDate LIKE DATE_SUB(STR_TO_DATE('2012-04-05','%d/%m/%Y'),INTERVAL 1 DAY);
回答by Tymoteusz Paul
Fairly simple when done with SQL, just add the condition
用 SQL 完成后相当简单,只需添加条件
WHERE creationDate BETWEEN CURDATE() - INTERVAL 1 DAY AND CURDATE()
There is no need to convert creationDate as it is already a date :). And i belive that this will be the fastest way to check it (which will matter if you go over large data sets).
无需转换 creationDate 因为它已经是一个日期 :)。而且我相信这将是检查它的最快方法(如果您查看大型数据集,这将很重要)。
回答by JMK
I think this should do it:
我认为应该这样做:
SELECT * FROM RECORD_DATA WHERE `creationDate` >= CURDATE()-1 and `creationDate` < CURDATE();
回答by John Woo
This will use INDEXon creationDate(if there is any).
这将使用INDEX上creationDate(如果有的话)。
SELECT *
FROM TableName
WHERE creationDate >= CURDATE() - INTERVAL 1 DAY AND
creationDate < CURDATE()
回答by Greg
The answers here seem to be answering 'How do I find something within the past 24 hours?', not 'How do I find something from the previous calendar date?'. Different questions.
这里的答案似乎是在回答“我如何在过去 24 小时内找到一些东西?”,而不是“我如何从上一个日历日期找到一些东西?”。不同的问题。
If you need to find something from the previous calendar date, you can try:
如果您需要查找上一个日历日期的内容,您可以尝试:
select creationDate from RECORD_DATA
where date(creationDate) = curdate() - INTERVAL 1 DAY;
No need to convert dates to strings and do string comparisons.
无需将日期转换为字符串并进行字符串比较。
回答by Isao
You can use
您可以使用
SELECT *
FROM `tableName`
WHERE DAY(created) = DAY(CURRENT_DATE - INTERVAL 1 DAY)

