[英]MySQL: Select date plus 6 months if less than now() plus 1 month
I have a given date: 我有给定的日期:
2013-12-20
This is an issue date of a product where the product expiries in 6 months from the date of issue. 这是产品的发布日期 ,其中产品自发布之日起六个月内到期。 The expiration date it is not stored so I want to check from the given issue date if it is expiring in the next month of now(). 它没有存储的到期日期,所以我想从给定的发行日期检查now()的下个月是否到期。
So my guess would be to add 6 months to the issue date and check now + interval of 1 month if it is expiring? 所以我的猜测是将6个月添加到发行日期,并立即检查+ 1个月的间隔是否到期?
Here is what I tried: 这是我尝试过的:
SELECT * FROM `products` WHERE DATE_ADD(`issue_date`,INTERVAL 6 MONTH)
< DATE(NOW() + INTERVAL 1 MONTH) ORDER BY `issue_date` ASC
What I'm I doing wrong? 我做错了什么?
SELECT * FROM products WHERE Now() <= DATE_ADD(issue_date, INTERVAL 7 MONTH) && Now() >= DATE_ADD(issue_date, INTERVAL 6 MONTH) ORDER BY issue_date ASC;
here's a suggestions which might help 这里的建议可能会有所帮助
$date = date("ymd", strtotime(" +6 months")); $ date = date(“ ymd”,strtotime(“ +6 months”));
this will result in date 6 months later from current date: 这将导致从当前日期起6个月后的日期:
similarly, 同样,
for one month from current date 从当前日期起一个月
$date = date("ymd", strtotime(" +1 months")); $ date = date(“ ymd”,strtotime(“ + 1个月”));
SELECT * FROM `products`
WHERE NOW() >= DATE_ADD(`issue_date`,INTERVAL 5 MONTH)
AND NOW() <= DATE_ADD(`issue_date`,INTERVAL 6 MONTH)
ORDER BY `issue_date` ASC
Your query says to select all the products that will expire within a month or had already expired. 您的查询说选择所有将在一个月内到期或已经到期的产品。 You could use between
clause 您可以使用between
子句
SELECT * FROM `products` WHERE DATE_ADD(`issue_date`,INTERVAL 6 MONTH) between NOW() and DATE(NOW() + INTERVAL 1 MONTH) ORDER BY `issue_date` ASC
If you want to check if the product expiries in 6 months from the date of issue and if it is expiring in the next month, the it is better to add this 1 month to the 6th in the query: 如果要检查产品自发布之日起6个月后是否到期,并且是否要在下个月到期,则最好将此1个月添加到查询的第6个月:
SELECT * FROM products WHERE issue_date <= DATE_ADD(issue_date,INTERVAL 7 MONTH)
ORDER BY issue_date ASC;
尝试这个
SELECT * FROM `products` where DATE_ADD(`issue_date`,INTERVAL 6 MONTH) < CURDATE()
After a few of these answers I ended up working it out with this: 经过以下几个回答,我最终对此进行了研究:
SELECT * FROM `products`
WHERE DATE_ADD( `issue_date` , INTERVAL 6 MONTH )
< DATE(NOW() + INTERVAL 1 MONTH)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.