使用多个JOIN时,Mysql返回错误的COUNT?

时间:2016-11-04 17:14:08

标签: mysql select join

我有3张桌子:NAMES,REGISTRATIONS和RENEWALS。我正在使用LEFT JOIN来加入具有公共ID的3个表。

我需要计算每个用户的注册数量以及续订数量。我尝试在GROUP BY字段中使用不同的选项,但似乎都没有。

这是SELECT语句:

SELECT 
    names.name_id AS 'Names ID'
    ,names.name AS Name
    ,count(registrations.date) AS Registrations
    ,count(renewals.date) AS Renewals

FROM names

LEFT JOIN registrations

    ON names.name_id = registrations.name_id

LEFT JOIN renewals

    ON renewals.name_id = registrations.name_id

GROUP BY names.name_id, registrations.name_id, renewals.name_id;

以下是3个表格:

    TABLE: names
    +---------+------+
    | name_id | name |
    +---------+------+
    |       1 | Ana  |
    |       2 | John |
    |       3 | Paul |
    +---------+------+

    TABLE: registrations
    +-----------------+---------+---------------------+-------+
    | registration_id | name_id | date                | value |
    +-----------------+---------+---------------------+-------+
    |               1 |       1 | 2014-01-30 13:15:02 | 15    |
    |               2 |       2 | 2014-05-01 18:01:44 | 15    |
    |               3 |       2 | 2014-07-08 15:10:43 | 20    |
    |               4 |       3 | 2012-09-28 17:45:32 | 15    |
    |               5 |       3 | 2014-01-09 18:26:14 | 20    |
    |               6 |       3 | 2015-01-10 13:22:01 | 25    |
    +-----------------+---------+---------------------+-------+

    TABLE: renewals
    +------------+---------+---------------------+-------+
    | renewal_id | name_id | date                | value |
    +------------+---------+---------------------+-------+
    |          1 |       1 | 2015-01-30 00:00:00 | 5     |
    |          2 |       1 | 2016-02-12 00:00:00 | 5     |
    |          3 |       1 | 2015-06-01 00:00:00 | 5     |
    |          4 |       1 | 2013-11-24 00:00:00 | 5     |
    |          5 |       2 | 2015-01-27 00:00:00 | 5     |
    +------------+---------+---------------------+-------+

这是我得到的INCORRECT结果:

    +----------+------+---------------+----------+
    | Names ID | Name | Registrations | Renewals |
    +----------+------+---------------+----------+
    |        1 | Ana  |             4 |        4 |
    |        2 | John |             2 |        2 |
    |        3 | Paul |             3 |        0 |
    +----------+------+---------------+----------+

我期待的正确结果是:

    +----------+------+---------------+----------+
    | Names ID | Name | Registrations | Renewals |
    +----------+------+---------------+----------+
    |        1 | Ana  |             1 |        4 |
    |        2 | John |             2 |        1 |
    |        3 | Paul |             3 |        0 |
    +----------+------+---------------+----------+

如何修复查询以获得正确的结果?

2 个答案:

答案 0 :(得分:2)

试试这个:

SELECT 
    names.name_id AS 'Names ID'
    ,names.name AS Name
    ,count(distinct registrations.registration_id) AS Registrations
    ,count(distinct renewals.renewal_id) AS Renewals
FROM names    
LEFT JOIN registrations    
    ON names.name_id = registrations.name_id    
LEFT JOIN renewals    
    ON renewals.name_id = registrations.name_id    
GROUP BY names.name_id, registrations.name_id, renewals.name_id;

每当遇到此类问题时,我发现如果您的服务器可以接受它,只需运行select *查询即可。像这样:

SELECT *
FROM names    
LEFT JOIN registrations    
    ON names.name_id = registrations.name_id    
LEFT JOIN renewals    
    ON renewals.name_id = registrations.name_id    ;

这会让你看到你真正在想什么。

答案 1 :(得分:0)

您的查询执行得很好。

第一次加入后,Ana有1个条目,John有2个条目,Paul有3个条目。

在几秒钟之后加入Ana的一个条目重复4次并加入(连接)4次续订。如果您现在计算Ana的注册日期,那么您将得到4.这就是您的“错误”来自的地方。

例如,您可以计算distinct日期来修复它。