获取 MySQL 中时间戳截止后的所有行

发布于 2024-12-27 12:50:51 字数 685 浏览 1 评论 0原文

我正在尝试从过去 30 天内具有 UNIX 时间戳的表中获取所有行。

SELECT count(member_id), DATE(FROM_UNIXTIME(`join_date`)) as date 
FROM members 
WHERE join_date > (join_date-'2592000')
GROUP BY DATE(FROM_UNIXTIME(`join_date`)) 
ORDER BY join_date DESC

显然,还有更多的事情发生,因为它按天对它们进行分组并将它们转换为 mysql 时间,但重要的是 where 子句不起作用.. 其余的都很好。

我没有收到任何错误,但查询没有按预期中断。

查询结果

编辑:明白了。

SELECT count(member_id), DATE(FROM_UNIXTIME(`join_date`)) as date 
FROM exp_members 
WHERE DATEDIFF(CURDATE(), FROM_UNIXTIME(join_date)) < 30
GROUP BY DATE(FROM_UNIXTIME(`join_date`)) 
ORDER BY join_date DESC

I'm trying to fetch all rows from a table with a UNIX timestamp in the past 30 days.

SELECT count(member_id), DATE(FROM_UNIXTIME(`join_date`)) as date 
FROM members 
WHERE join_date > (join_date-'2592000')
GROUP BY DATE(FROM_UNIXTIME(`join_date`)) 
ORDER BY join_date DESC

There's more going on there, obviously, as it groups them by day and converts them to a mysql time, but the important thing is the where clause that's not working.. The rest of it is good.

I'm not getting any errors, but the query isn't cutting off as expected.

Query Result

EDIT: Got it.

SELECT count(member_id), DATE(FROM_UNIXTIME(`join_date`)) as date 
FROM exp_members 
WHERE DATEDIFF(CURDATE(), FROM_UNIXTIME(join_date)) < 30
GROUP BY DATE(FROM_UNIXTIME(`join_date`)) 
ORDER BY join_date DESC

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野心澎湃 2025-01-03 12:50:51

这个: WHERE join_date > (join_date-'2592000') 几乎肯定是您的问题。

您可能想使用 DATEDIFF 将 join_date 与 CURDATE (而不是它本身-x)进行比较,这样您就可以使用全天增量而不是秒作为 259200,这对大多数人来说更好 -可读性。

This: WHERE join_date > (join_date-'2592000') is almost certainly your issue.

You probably want to compare join_date to CURDATE (rather than itself-x) using DATEDIFF that way you can use whole day increments instead of seconds as 259200 which is better most human-readable-wise.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文