php MYSQL SELECT 记录早于 1 年前
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32886852/
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
MYSQL SELECT records older than 1 year ago
提问by Alex Grecu
I try to get a list of all records that are older than 1year ago from my database, the field for expired_contract has next information.
我尝试从我的数据库中获取早于 1 年的所有记录的列表,expired_contract 的字段包含下一个信息。
expired_contract DATE NOT NULL
So it takes the DATE in the next format: YEAR-MM-DD, next i have the sql that i cant get it working sadly.
所以它采用下一种格式的日期:YEAR-MM-DD,接下来我有一个我无法让它工作的sql。
$sql = "SELECT *
FROM My_Contracte
WHERE expired_contract >= DATE_SUB(NOW(),INTERVAL 1 YEAR)
ORDER BY id_contract DESC";
I tried a lot of "WHERE" commands but none worked as i expected. Can you help me get this working? I'm looking on this for about 5hours i need exact command to get it worked.
我尝试了很多“WHERE”命令,但没有一个像我预期的那样工作。你能帮我解决这个问题吗?我正在研究这个大约 5 个小时,我需要确切的命令才能使其正常工作。
The $sql gets me something but takes it wrong, i get dates like: 2015-10-01, 2016-10-01 and date like 2014-09-30 doesn't show up.
$sql 给了我一些东西,但它出错了,我得到的日期是:2015-10-01、2016-10-01 和 2014-09-30 之类的日期没有出现。
Basically i want to show dates like:
基本上我想显示日期,如:
If today is 2015-10-01 i want to see dates older than 1year ago so from 2014-09-30 and not showing dates like 2015-10-01, 2016-10-01.
如果今天是 2015-10-01,我想看到早于 1 年前的日期,所以从 2014-09-30 开始,而不是显示像 2015-10-01、2016-10-01 这样的日期。
Maybe do i have to edit something in database?
也许我必须在数据库中编辑一些东西?
Looking for your help, thank you!
寻求您的帮助,谢谢!
回答by Jens
You have to use lower than instead of greater or equals:
您必须使用小于而不是大于或等于:
$sql = "SELECT * FROM My_Contracte WHERE expired_contract < DATE_SUB(NOW(),INTERVAL 1 YEAR)
回答by Master Yoda
SELECT * FROM My_Contracte WHERE (expired_contract NOT BETWEEN DATE_SUB(CURDATE(),INTERVAL 1 YEAR) AND CURDATE()) and expired_contract<=NOW() ;
回答by Mani
SELECT SUM(impressions) AS impressions FROM My_Contracte where DATE(expired_contract) BETWEEN DATE_SUB( CURDATE( ) ,INTERVAL 1 YEAR ) AND CURDATE( )