所以,我在使用PHP和Mysql有效构建邮件的提要样式列表时出现问题。 我希望根据发布日期来组织和分组帖子,并将日期显示为标题。例如:从发布之日起将mysql中的项目分组
February 9th -----------
Posts that were posted on Feb 9th
Feb 8th -----------------
Posts that were posted on the 8th
希望这是明确的。如果有人感到困惑,我会尽力澄清事情。
所以,我在使用PHP和Mysql有效构建邮件的提要样式列表时出现问题。 我希望根据发布日期来组织和分组帖子,并将日期显示为标题。例如:从发布之日起将mysql中的项目分组
February 9th -----------
Posts that were posted on Feb 9th
Feb 8th -----------------
Posts that were posted on the 8th
希望这是明确的。如果有人感到困惑,我会尽力澄清事情。
问题中的标签暗示使用“GROUP BY”。你做不是需要SQL GROUP BY构造。相反,查询应该是这个样子:
SELECT *
FROM MyPostsTable
--WHERE some optional condition
ORDER BY PostingDate -- DESC (if you want the newer dates first)
然后你通过resuults列表迭代,在PHP中,你只需要保持项目的日期目前正在输出的选项卡,并放出排序的头(“2月9日----------”),在新的日期开始。
编辑:请参阅Matt Bridges对上述逻辑示例实现的回答。
$sql = "SELECT PostDataColumn1, PostDataColumn2, date_format('%Y-%m-%d', DateColumn) FROM posts_table ORDER BY DateColumn DESC";
$result = mysql_query($sql);
$curDate = "";
while (list($col1, $col2, $date) = mysql_fetch_row($result))
{
if ($date != $curDate)
{
echo "$date --------\n";
$curDate = $date;
}
echo "Post data: $col1 $col2";
}
+1提供的PHP代码,而不是像我一样解释它的一般逻辑;-) – mjv 2010-02-09 01:31:42
这是我最终使用的方法。谢谢你的帮助! – Dandy 2010-02-09 00:20:15
请问您可以发布您使用的代码吗?我试图解决这个问题,这让我疯狂! :( – 2012-01-13 02:09:53