日期算術

NOW() + INTERVAL 1 DAY  -- This time tomorrow

CURDATE() - INTERVAL 4 DAY -- Midnight 4 mornings ago

顯示 3 到 10 小時前(180 到 600 分鐘前)詢問的儲存的 mysql 問題:

SELECT qId,askDate,minuteDiff  
FROM 
(   SELECT qId,askDate,
    TIMESTAMPDIFF(MINUTE,askDate,now()) as minuteDiff 
    FROM questions_mysql 
) xDerived 
WHERE minuteDiff BETWEEN 180 AND 600 
ORDER BY qId DESC 
LIMIT 50;

+----------+---------------------+------------+
| `qId`      | askDate             | minuteDiff |
+----------+---------------------+------------+
| `38546828` | 2016-07-23 22:06:50 |        182 |
| `38546733` | 2016-07-23 21:53:26 |        195 |
| `38546707` | 2016-07-23 21:48:46 |        200 |
| `38546687` | 2016-07-23 21:45:26 |        203 |
| ...      |                     |            |
+----------+---------------------+------------+

用於 TIMESTAMPDIFF() MySQL 手冊頁。

注意不要嘗試在 CURDATE() + 1 中使用像 CURDATE() + 1 這樣的表示式來進行日期演算法。它們不會返回你的期望,特別是如果你習慣使用 Oracle 資料庫產品。請改用 CURDATE() + INTERVAL 1 DAY