於 MySQL 要計算抓取 6個月前的日期, 要如何做?
使用 MySQL 計算 6個月前的日期
使用 DATE_SUB() 或 SUBDATE() 都是一樣的.
- SELECT DATE_SUB(DATE(NOW()), INTERVAL 6 MONTH); # 6個月前
+-----------------------------------------+
| DATE_SUB(DATE(NOW()), INTERVAL 6 MONTH) |
+-----------------------------------------+
| 2013-01-23 |
+-----------------------------------------+ - SELECT DATE_SUB('2008-01-02', INTERVAL 31 DAY); # 31天前+-----------------------------------------+
| DATE_SUB('2008-01-02', INTERVAL 31 DAY) |
+-----------------------------------------+
| 2007-12-02 |
+-----------------------------------------+ - SELECT SUBDATE('2008-01-02', INTERVAL 31 DAY);+-----------------------------------------+
| DATE_SUB('2008-01-02', INTERVAL 31 DAY) |
+-----------------------------------------+
| 2007-12-02 |
+-----------------------------------------+