日期算术

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