我有两个表:
reports:
date, uuid
warns:
date, uuid, active
date
是时间戳 (2016-05-16 16:06:58),uuid
是用户标识符字符串, active
是一个 bool 值。
我想显示每天有多少 reports
和 warns
条目。目前我有这个查询:
SELECT DATE(date) Date, count(*) Reports
FROM reports
GROUP BY DATE(date)
ORDER BY DATE(date) DESC
显示如下表格:
Date | Reports
-----------+---------
2016-07-05 | 192
2016-07-04 | 230
2016-07-03 | 227
但我也想知道那天发生了多少 warns
条目,但前提是 active
列是 TRUE
,所以我想返回这样一个表的查询:
Date | Reports | Warns
-----------+---------+-------
2016-07-05 | 192 | 47
2016-07-04 | 230 | 59
2016-07-03 | 227 | 56
我是 MYSQL 的新手,所以我还不知道如何做。我在 JOINS 和 UNIONS 上搜索了一下,但不知道它们是否/如何适用于我的案例。任何帮助将不胜感激。
最佳答案
您可以使用相关查询:
SELECT DATE(reports.date) Date, count(*) Reports,
(SELECT COUNT(*) FROM warns
WHERE DATE(warns.date) = DATE(reports.date) and warns.active = 'TRUE') as warns_cnt
FROM reports
GROUP BY DATE(reports.date)
ORDER BY DATE(reports.date) DESC
关于mysql - 每天统计来自不同表的条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38221841/