按日期时间戳分组 MySQL
我对时间戳进行了以下查询(由 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要有某个地方的日期列表。许多人为此目的创建一个日历表,其中包含他们需要的每个日期(或每个值),以便可以将该日历表和您的数据表连接起来以提供此类查询。
从长远来看,您会发现这比任何其他解决方案都要简单得多,只需花一点时间即可设置。
有关如何生成此类的示例,请参阅本文一张桌子。
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.