MYSQL SELECT记录早于1年前 [英] MYSQL SELECT records older than 1 year ago

查看:115
本文介绍了MYSQL SELECT记录早于1年前的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从数据库中获取所有早于1年的记录的列表,expired_contract字段具有下一个信息.

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.

expired_contract DATE NOT NULL

所以它采用以下格式的DATE:YEAR-MM-DD,接下来我有一个SQL,我很难使它工作.

So it takes the DATE in the next format: YEAR-MM-DD, next i have the sql that i cant get it working sadly.

$sql = "SELECT * 
        FROM My_Contracte 
        WHERE expired_contract >= DATE_SUB(NOW(),INTERVAL 1 YEAR) 
        ORDER BY id_contract DESC";

我尝试了很多"WHERE"命令,但没有一个能按预期工作.您能帮我解决这个问题吗?我正在寻找这个大约5个小时,我需要确切的命令才能使其正常工作.

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.

$ sql为我提供了一些帮助,但是却弄错了,我没有显示日期:2015-10-01、2016-10-01和2014-09-30这样的日期.

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.

基本上我想显示以下日期:

Basically i want to show dates like:

如果今天是2015年10月1日,我想查看1年之前的日期,那么从2014年9月30日开始,不显示2015年10月1日,2016年10月1日之类的日期.

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.

也许我需要编辑数据库中的某些内容吗?

Maybe do i have to edit something in database?

期待您的帮助,谢谢!

推荐答案

您必须使用小于而不是大于或等于:

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) 

这篇关于MYSQL SELECT记录早于1年前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆