按日期时间戳分组 MySQL

发布于 2024-11-06 17:00:59 字数 358 浏览 1 评论 0原文

我对时间戳进行了以下查询(由 INT(10) 指定,这符合我的要求,但不完全是:

SELECT count(entry_date) as theCount, FROM_UNIXTIME(entry_date, '%Y-%m-%d') AS dd 
FROM exp_weblog_titles 
WHERE entry_date < UNIX_TIMESTAMP(NOW()) 
GROUP BY dd 
ORDER BY dd DESC 
LIMIT 7

吐出最后 7 个日期以及今天日期之前的这些日期的条目数我创建的查询的问题是,它只显示日期,如果它实际上有一个条目,实际上,我仍然希望包含该日期,即使有零个条目,这可能吗?

I've got the following query on a timestamp(specified by INT(10), which kind of does what I want, but not exactly:

SELECT count(entry_date) as theCount, FROM_UNIXTIME(entry_date, '%Y-%m-%d') AS dd 
FROM exp_weblog_titles 
WHERE entry_date < UNIX_TIMESTAMP(NOW()) 
GROUP BY dd 
ORDER BY dd DESC 
LIMIT 7

That spits out the last 7 dates and the # of entries on those dates that are before today's date. The problem with the query I've created is, it only spits out the date if it actually has an entry on it. When in actuality, I still want that date included even if there are zero entries. Is that possible?

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

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

发布评论

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

评论(1

披肩女神 2024-11-13 17:00:59

您需要有某个地方的日期列表。许多人为此目的创建一个日历表,其中包含他们需要的每个日期(或每个值),以便可以将该日历表和您的数据表连接起来以提供此类查询。

从长远来看,您会发现这比任何其他解决方案都要简单得多,只需花一点时间即可设置。

有关如何生成此类的示例,请参阅本文一张桌子。

You will need to have a list of dates from somewhere. A lot of people create a calendar table just for this purpose with every date (or every value) that they need so that this calendar table and your data table can be joined to provide this kind of query.

You will find in the long run this is far simpler than any other solution, just takes a moment to set up.

See this article for an example of how to generate such a table.

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