按月列出博客条目

发布于 2024-08-08 11:30:31 字数 121 浏览 2 评论 0原文

我正在编写自定义代码来创建博客。我需要档案页面按月列出所有博客条目。我想不出办法做到这一点。 我想这应该不会太难,因为它是所有博客的共同功能。 表结构是(postid,posttitle,publishdate,......)

I am writing custom code to create a blog. I need the archives page to list all the blog entries by month. I cannot come up with a way to do that.
I guess it should not be too tough as it is a common feature on all blogs.
The table structure is (postid, posttitle, publishdate, .....)

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

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

发布评论

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

评论(3

笔芯 2024-08-15 11:30:31

我不确定我是否理解这个问题,但如果您只想要每月所有帖子的数量,请使用如下查询:

SELECT DATE_FORMAT(publishdate, '%Y%m') AS publishmonth, count(*) AS entrycount
FROM entries GROUP BY DATE_FORMAT(publishdate, '%Y%m')

如果您想要特定月份的所有帖子:

SELECT * FROM entries WHERE publishdate > '2009-01' AND publishdate < '2009-02';

并且如果您想列出全部 在单个页面上按月份分组的帖子,只需选择按发布日期排序的帖子并在本地进行分组即可。

I'm not sure I understand the question, but if you want just numbers of all posts per month, use a query like this:

SELECT DATE_FORMAT(publishdate, '%Y%m') AS publishmonth, count(*) AS entrycount
FROM entries GROUP BY DATE_FORMAT(publishdate, '%Y%m')

If you want all posts for a particular month:

SELECT * FROM entries WHERE publishdate > '2009-01' AND publishdate < '2009-02';

And if you want to list all posts grouped by month on a single page, just select them sorted by publishdate and do the grouping locally.

开始看清了 2024-08-15 11:30:31

如果您的条目来自 SQL 数据库,最简单的方法是要求该数据库使用 ORDER BY 为您执行排序。像这样的东西

select * from posts order by publishdate

If your entries come from a SQL database, it's easiest to ask that to perform the sort for you using an ORDER BY. Something like

select * from posts order by publishdate
超可爱的懒熊 2024-08-15 11:30:31

像这样的伪代码:

SELECT `publishdate` FROM `entries` ORDER BY DESC `publishdate` GROUP BY YEAR(`publishdate`), MONTH(`publishdate`);
foreach ($dates as $date) {
    $date = mysql_real_escape_string($date)
    SELECT * FROM `entries` WHERE `publishdate` = $date
}

我想。

Something like this pseudo-code:

SELECT `publishdate` FROM `entries` ORDER BY DESC `publishdate` GROUP BY YEAR(`publishdate`), MONTH(`publishdate`);
foreach ($dates as $date) {
    $date = mysql_real_escape_string($date)
    SELECT * FROM `entries` WHERE `publishdate` = $date
}

I think.

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