gpt4 book ai didi

mysql - 在此 MySQL 查询中对记录进行分组和求和

转载 作者:行者123 更新时间:2023-11-30 23:09:42 25 4
gpt4 key购买 nike

我有两个连接表,“reports”和“visits”,以及这样的查询:

SELECT reports.rep_dated,
(SELECT COUNT(*) FROM visits
WHERE visits.vis_report = reports.rep_id) AS vis_count
FROM reports
HAVING vis_count > 0
ORDER BY rep_dated

返回以下数据:

+------------+-----------+
| rep_dated | vis_count |
+------------+-----------+
| 2013-11-03 | 3 |
| 2013-11-07 | 4 |
| 2013-11-28 | 2 |
| 2013-11-28 | 3 |
+------------+-----------+

到目前为止一切顺利(您看到 rep_dated 可以重复,因为它不是主键:那是因为第一个“2013-11-28”指的是上午完成的访问,第二个指的是下午完成的访问)

现在我想对同一天的访问进行求和(也就是对上午 + 下午完成的访问进行求和),并返回:

+------------+-----------+
| rep_dated | vis_count |
+------------+-----------+
| 2013-11-03 | 3 |
| 2013-11-07 | 4 |
| 2013-11-28 | 5 |
+------------+-----------+

而且,正如您已经想象的那样,这就是我卡住的地方......

GROUP BY reports.rep_dated 不起作用...我怎样才能得到那个集合?
预先感谢您的帮助!

最佳答案

试试这个:

SELECT reports.rep_dated,
COUNT(*) AS vis_count
FROM VISITS
JOIN reports ON visits.vis_report = reports.rep_id
GROUP BY reports.rep_dated
ORDER BY reports.rep_dated

关于mysql - 在此 MySQL 查询中对记录进行分组和求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20372463/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com