MySQL SELECT 最近几天?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1713056/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 14:29:13  来源:igfitidea点击:

MySQL SELECT last few days?

mysqlsql

提问by Strawberry

I was playing with MYSQL and I know there's a limit command that shows a certain amount of results, but i was wondering if MySQL alone can show only the last 3 days or something. Just wondering.

我在玩 MYSQL,我知道有一个限制命令可以显示一定数量的结果,但我想知道单独的 MySQL 是否只能显示过去 3 天之类的。就是想。

Update:I used NOW() to store times.

更新:我使用 NOW() 来存储时间。

回答by OMG Ponies

Use for a date three days ago:

用于三天前的约会:

WHERE t.date >= DATE_ADD(CURDATE(), INTERVAL -3 DAY);

Check the DATE_ADDdocumentation.

检查DATE_ADD文档。

Or you can use:

或者你可以使用:

WHERE t.date >= ( CURDATE() - INTERVAL 3 DAY )

回答by PythonDev

You can use this in your MySQL WHERE clause to return records that were created within the last 7 days/week:

您可以在 MySQL WHERE 子句中使用它来返回过去 7 天/周内创建的记录:

created >= DATE_SUB(CURDATE(),INTERVAL 7 day)

created >= DATE_SUB(CURDATE(),INTERVAL 7 day)

Also use NOW() in the subtraction to give hh:mm:ss resolution. So to return records created exactly (to the second) within the last 24hrs, you could do:

在减法中也使用 NOW() 给出 hh:mm:ss 分辨率。因此,要返回在过去 24 小时内准确创建(到第二个)的记录,您可以执行以下操作:

created >= DATE_SUB(NOW(),INTERVAL 1 day)

created >= DATE_SUB(NOW(),INTERVAL 1 day)

回答by Turnor

You could use a combination of the UNIX_TIMESTAMP() function to do that.

您可以使用 UNIX_TIMESTAMP() 函数的组合来做到这一点。

SELECT ... FROM ... WHERE UNIX_TIMESTAMP() - UNIX_TIMESTAMP(thefield) < 259200

回答by ino

WHERE t.date >= DATE_ADD(CURDATE(), INTERVAL '-3' DAY);

WHERE t.date >= DATE_ADD(CURDATE(), INTERVAL '-3' DAY);

use quotes on the -3 value

在 -3 值上使用引号

回答by Vikas Kumar

SELECT DATEDIFF(NOW(),pickup_date) AS noofday 
FROM cir_order 
WHERE DATEDIFF(NOW(),pickup_date)>2;

or

或者

SELECT * 
FROM cir_order 
WHERE cir_order.`cir_date` >= DATE_ADD( CURDATE(), INTERVAL -10 DAY )