mysql - 每天统计来自不同表的条目

标签 mysql

我有两个表:

reports:
    date, uuid
warns:
    date, uuid, active

date 是时间戳 (2016-05-16 16:06:58)uuid 是用户标识符字符串, active 是一个 bool 值。

我想显示每天有多少 reportswarns 条目。目前我有这个查询:

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/

相关文章:

mysql - JDBC 接收器连接器 : How to map fields from the Kafka's message to the database table's column

mysql - 我是否需要一个针对具有 20 多个属性的模型的搜索引擎

mysql - 需要将 orderby 函数与 groupby 函数一起使用

php - MySQL => MariaDB = PHP 错误头文件/库版本

php - 如何将 PHP 变量数组发送到 MySQL 数据库?

mysql - 如何连接 MySQL 和 python 3

mysql - 在 perl DBD::mysql 中使用 --login-path=local

php - 在php中插入数据时出错

mysql - 如何根据 2 列条件计算 SUM

mysql - 使用托管商上托管的 phpMyadmin 从我的本地系统连接到 MySql 数据库