我有一个表,其中一行名为 time
,类型为 DATETIME
。
如何选择“存活”时间不超过 5 天的所有行?
或者换句话说,我如何获取时间戳不超过 5 天前的数据?
我的要求:
SELECT *
FROM `stats`
WHERE `domain` = 'test.com' AND DATEADD(NOW(), interval -5 day) > 'accept_date'
accept_date
是一行的真实名称。因此,使用该代码我得到了:
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') > 'accept_date' LIMIT 0, 30' at line 1
最佳答案
这取决于 RDBMS 但在 MySQL 中它会是
where date_add(now(), interval -5 day) > time
或
where datediff(now(), time) < 5
第一个表示“120 小时后”,第二个表示“5 个日历日后”。
顺便说一句,time
是一个糟糕的列名。什么时间?
关于mysql - SQL 和 DateTime - 如何选择在特定时间段内存在的所有行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6718414/