回响每组的行数

时间:2013-11-18 11:49:38

标签: php mysql count timestamp

我有一个mysql查询,其中我按发布的日期对每个帖子进行分组。我希望能够回应每天发生的帖子数量,知道一种方法吗?

这是我到目前为止所写的内容,它会在发布的每一天发布一个新的<div>

$trendQuery = "
SELECT DATE(timestamp) AS ForDate,
    COUNT(*) AS NumPosts
 FROM   trends
 WHERE `trend` = '" . $_GET['graph'] . "'
 GROUP BY DATE(timestamp)
 ORDER BY ForDate
";

$result = mysql_query($trendQuery);
    while ($row = mysql_fetch_array($result)) {
        echo'
        <div>
            <p>Here is where I want to say how many posts that happened!</p>
    </div>
    ';
}

2 个答案:

答案 0 :(得分:1)

您的查询设置非常多。要回显结果,您只需引用带有别名的列,例如ForDateNumPosts

$trendQuery = "
SELECT timestamp AS ForDate,
    COUNT(*) AS NumPosts
 FROM   trends
 WHERE `trend` = '" . $_GET['graph'] . "'
 GROUP BY timestamp
 ORDER BY ForDate
";

$result = mysql_query($trendQuery);
    while ($row = mysql_fetch_array($result)) {
        echo'
        <div>
            Date: '.$row['ForDate'].' ---- Number of Posts: '.$row['NumPosts'].'<br />
    </div>
    ';

答案 1 :(得分:0)

创建趋势查询的视图,并使用“选择(您要合并的ID或日期)从内部加入”趋势“

我希望有帮助