SQL:查询时间戳> = 30天前的条目

时间:2017-05-10 19:01:32

标签: sql

我的参赛作品有时间戳,如1488446089,1469525148等。

我正在尝试获取不超过30天的条目。 另一个用例是获取条目> =年(2016)

这个查询有什么问题。它返回所有结果,忽略where子句

"SELECT DATE(FROM_UNIXTIME(createdTime)) AS ForDate,
          COUNT(*) AS NumPosts
          FROM  Posts
          WHERE DATE_SUB(from_unixtime(createdTime), INTERVAL 30 DAY)
          GROUP BY DATE(FROM_UNIXTIME(createdTime))
          ORDER BY ForDate"

1 个答案:

答案 0 :(得分:2)

似乎你没有过滤器(yoy不比较值),例如date_sub(...) >= DATE(FROM_UNIXTIME(createdTime))

"SELECT DATE(FROM_UNIXTIME(createdTime)) AS ForDate,
        COUNT(*) AS NumPosts
        FROM  Posts
 WHERE DATE_SUB(from_unixtime(createdTime), INTERVAL 30 DAY) >= DATE(FROM_UNIXTIME(createdTime))
        GROUP BY DATE(FROM_UNIXTIME(createdTime))
        ORDER BY ForDate"