尝试按月将结果排序并显示在表格中。我哪里错了?
查询:
mysql_query("SELECT COUNT(*), DATE_FORMAT(DATE(`dPostDateTime`), '%W, %b, %e') AS `day`, DATE(`dPostDateTime`) AS 'date' FROM `tblQA` WHERE cCategory IN ('Football','Baseball','Basketball','Hockey') AND dPostDateTime >= '2010-08-01' AND dPostDateTime <= '2011-07-31' GROUP BY `month`");
PHP代码:
while($row = mysql_fetch_assoc($monthlyQ))
{
$printedRecords1++;
echo "<tr class='forum'>";
echo "<td class='forum'>" . $row['month'] . "</td>";
echo "<td class='forum'>" . $row['COUNT(*)'] . "</td>";
echo "</tr>";
}
for ($i = $printedRecords1; $i < $Print; $i++) {
echo "<tr class='forum'>";
echo "<td class='forum'> </td>";
echo "<td class='forum'> </td>";
echo "</tr>";
}
echo "</table></td><td>";
答案 0 :(得分:0)
如果您希望按月分类,则需要添加
ORDER BY `month`
到你的SQL
答案 1 :(得分:0)
我假设当你说你试图按月分类时,你只是意味着你要弄清楚每个月有多少行,因为这看起来你的代码正在做什么。
如果你的表中确实有一个'month'字段,那么你的SQL很好,除了你实际上从未选择过那个字段,因此$row['month']
不应该有一个SQL值。
所以你需要选择它如下:
mysql_query("SELECT COUNT(*), `month`, DATE_FORMAT(DATE(`dPostDateTime`), '%W, %b, %e') AS `day`, DATE(`dPostDateTime`) AS 'date' FROM `tblQA` WHERE cCategory IN ('Football','Baseball','Basketball','Hockey') AND dPostDateTime >= '2010-08-01' AND dPostDateTime <= '2011-07-31' GROUP BY `month`");
如果您没有“月份”字段(并且只有日期字段,这意味着您的查询不应该有效),您需要以与“日期”类似的方式创建该字段
mysql_query("SELECT COUNT(*), MONTH(`dPostDateTime`) AS `month`, DATE_FORMAT(DATE(`dPostDateTime`), '%W, %b, %e') AS `day`, DATE(`dPostDateTime`) AS 'date' FROM `tblQA` WHERE cCategory IN ('Football','Baseball','Basketball','Hockey') AND dPostDateTime >= '2010-08-01' AND dPostDateTime <= '2011-07-31' GROUP BY `month`");
然后,如果您希望您的结果按月排序,那么表格按照时间顺序排列,您也希望在其中添加ORDER BY month
,假设它存在(如示例1)或者您正在创建它(如示例2所示)。