开发者

Subtract month and day mysql

I need to subtract 1 month and 开发者_运维问答4 days with mysql, I saw the command DATE_ADD (NOW (), - 1 MONTH) perfect for 1 month but for 1 month and 4 days, using 31 days is not valid for every month that some bring 30, 29, 28. I can not add 31 + 4, 30 + 4, etc.


using DATE_SUB [docs] like :

DATE_SUB((DATE_SUB(curdate(), INTERVAL 1 MONTH)), INTERVAL 4 DAY)


SELECT DATE_ADD(DATE_ADD(NOW(),INTERVAL -1 MONTH), INTERVAL -4 DAY)


Keep it simple:

SELECT CURDATE() - INTERVAL 1 MONTH - INTERVAL 4 DAY;

or

SELECT '2014-03-27' - INTERVAL 1 MONTH - INTERVAL 4 DAY;

or if you like to preserve the current time:

SELECT NOW() - INTERVAL 1 MONTH - INTERVAL 4 DAY;

(Tested on MySQL 5.1.73 and newer)


SELECT CURRENT_TIMESTAMP + INTERVAL - 1 MONTH + INTERVAL - 4 DAY;
or
SELECT CURRENT_DATE + INTERVAL - 1 MONTH + INTERVAL - 4 DAY;

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜