是否需要先按 2 列分组,再按 1 列分组进行 2 次查询?

发布于 2024-12-04 04:54:13 字数 1074 浏览 1 评论 0原文

我有一个包含电子邮件地址的表(列:id、电子邮件、用户、日期)。我正在尝试按日期和用户对电子邮件地址进行求和,我可以使用下面的代码来完成此操作;但随后还要对所有用户的总数进行求和并将其显示在该日期下方。我不知道该怎么做...我需要再做一次查询来计算每个日期的总计吗?

$sql = mysql_query("SELECT date, COUNT(email), user FROM emails GROUP BY DATE(date), user");
while ($row = mysql_fetch_array($sql)){
  echo date('m/d', strtotime($row['date'])) . " " . $row['user'] . " " . $row['COUNT(email)'] . "<br />";
}

我拥有什么:

date user count(email)
09/09 29 8
09/09 49 9
09/10 29 4
09/10 49 13
09/11 29 1
09/11 49 3

我想要什么:

date user count(email)
09/09 29 8
09/09 49 9
09/09 total 17

09/10 29 5
09/10 49 13
09/10 total 18

09/11 29 1
09/11 49 3
09/11 total 4

谢谢

编辑:这是我的有效代码:

$sql = mysql_query("SELECT date, COUNT(email), user FROM emails GROUP BY DATE(date), user WITH ROLLUP");

while ($row = mysql_fetch_array($sql)){
  echo date('m/d', strtotime($row['date'])) . " " . (!isset($row['user']) ? 'total' : $row['user']) . " " . $row['COUNT(email)'] . "<br />";
}

I have a table with email addresses (colums: id, email, user, date). I'm trying to sum email addresses by date then user, which I'm able to do with the below code; but then also sum the total for all users and display it below that date. I'm not sure how to do that... do I need to do another query to total for each date?

$sql = mysql_query("SELECT date, COUNT(email), user FROM emails GROUP BY DATE(date), user");
while ($row = mysql_fetch_array($sql)){
  echo date('m/d', strtotime($row['date'])) . " " . $row['user'] . " " . $row['COUNT(email)'] . "<br />";
}

What I have:

date user count(email)
09/09 29 8
09/09 49 9
09/10 29 4
09/10 49 13
09/11 29 1
09/11 49 3

What I would like:

date user count(email)
09/09 29 8
09/09 49 9
09/09 total 17

09/10 29 5
09/10 49 13
09/10 total 18

09/11 29 1
09/11 49 3
09/11 total 4

Thanks

EDIT: Here's my code that works:

$sql = mysql_query("SELECT date, COUNT(email), user FROM emails GROUP BY DATE(date), user WITH ROLLUP");

while ($row = mysql_fetch_array($sql)){
  echo date('m/d', strtotime($row['date'])) . " " . (!isset($row['user']) ? 'total' : $row['user']) . " " . $row['COUNT(email)'] . "<br />";
}

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

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

发布评论

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

评论(2

彻夜缠绵 2024-12-11 04:54:13

请参阅使用 ROLLUP GROUP BY 修饰符)。

SELECT DATE(`date`) AS date
     , user 
     , COUNT(email) AS cnt
FROM emails 
GROUP BY DATE(`date`)
       , user 
WITH ROLLUP 

会给你:

date    user   cnt
09/09   29       8
09/09   49       9
09/09   NULL    17

09/10   29       5
09/10   49      13
09/10   NULL    18 

09/11   29       1
09/11   49       3
09/11   NULL     4

NULL    NULL    39

See WITH ROLLUP (a GROUP BY modifier).

SELECT DATE(`date`) AS date
     , user 
     , COUNT(email) AS cnt
FROM emails 
GROUP BY DATE(`date`)
       , user 
WITH ROLLUP 

would give you:

date    user   cnt
09/09   29       8
09/09   49       9
09/09   NULL    17

09/10   29       5
09/10   49      13
09/10   NULL    18 

09/11   29       1
09/11   49       3
09/11   NULL     4

NULL    NULL    39
梦年海沫深 2024-12-11 04:54:13

我认为你可以使用 UNION ALL 来做到这一点。它是这样的:

SELECT date, COUNT(email), user
FROM emails
GROUP BY DATE(date), user
UNION ALL
SELECT date, COUNT(email), -1
FROM emails
GROUP BY DATE(date)
ORDER date ASC

或者你可以用 PHP 方式:

$previousDate = null;
$total = 0;
$sql = mysql_query("SELECT date, COUNT(email), user FROM emails GROUP BY DATE(date), user");
while ($row = mysql_fetch_array($sql)){
    if ($previousDate != date('m/d', strtotime($row['date']))) {
        echo $previousDate . " total " . $total . "<br /><br />";
        $total = 0;
        $previousDate = date('m/d', strtotime($row['date']));
    }

    echo date('m/d', strtotime($row['date'])) . " " . $row['user'] . " " . $row['COUNT(email)'] . "<br />";
    $total = $total + $row['COUNT(email)'];
}

I think you can use UNION ALL to do this. It goes something like this:

SELECT date, COUNT(email), user
FROM emails
GROUP BY DATE(date), user
UNION ALL
SELECT date, COUNT(email), -1
FROM emails
GROUP BY DATE(date)
ORDER date ASC

Or you can go in the PHP way:

$previousDate = null;
$total = 0;
$sql = mysql_query("SELECT date, COUNT(email), user FROM emails GROUP BY DATE(date), user");
while ($row = mysql_fetch_array($sql)){
    if ($previousDate != date('m/d', strtotime($row['date']))) {
        echo $previousDate . " total " . $total . "<br /><br />";
        $total = 0;
        $previousDate = date('m/d', strtotime($row['date']));
    }

    echo date('m/d', strtotime($row['date'])) . " " . $row['user'] . " " . $row['COUNT(email)'] . "<br />";
    $total = $total + $row['COUNT(email)'];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文