mysql - SQL 和 DateTime - 如何选择在特定时间段内存在的所有行?

标签 mysql sql datetime timestamp

我有一个表,其中一行名为 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/

相关文章:

php - 如何允许一名选民投票给多名候选人,但不允许他再次投票给同一候选人?

c# - 安装的应用程序无法加载MySQL.Data.DLL?

javascript - MomentJS 的奇怪日期时间输出

python - 将日期时间对象传递给 django 中的 url

python - 名称错误 : name 'UTC' is not defined

mysql - 大表的SQL优化和基于键的选择

mysql - 在 MySQL 中排序 PayPal 日期

sql - 将我的列转换为 postgresql 中的行而不创建任何扩展

sql - 使用 SQL 脚本创建用户、映射到数据库并为该数据库分配角色

mysql - 多种条件下的多次更新