MySQL连接和COUNT()多个表

时间:2015-11-01 03:40:46

标签: mysql sql count sum left-join

我正在尝试在一个查询中COUNT()多个表,但我无法让它工作。这是我到目前为止所拥有的。

TABLES

table1
---------------------
id | name
---------------------
 1 | test
 2 | test2


table2
---------------------
id | table1_id
---------------------
 1 | 1
 2 | 1
 3 | 1


table3
---------------------
id | table2_id
---------------------
 1 | 1


table4
---------------------
id | size | table3_id
---------------------
 1 | 1024 | 1
 1 | 200  | 1

SQL:

SELECT
    table1.name,
    COUNT(table2.table1_id) AS table2_count,
    COUNT(table3.table2_id) AS table3_count,
    COUNT(table4.table3_id) AS table4_count,
    SUM(table4.size) AS table4_size
FROM
    table1
LEFT JOIN table2
    ON table1.id = table2.table1_id
LEFT JOIN table3
    ON table2.id = table3.table2_id
LEFT JOIN table4
    ON table3.id = table4.table3_id
WHERE
    table1.id = 1

我从以上查询得到的结果:

name | table2_count | table3_count | table4_count | table4_size
---------------------------------------------------------------
test |      4       |      2       |      2       |    1224

我应该得到的结果:

name | table2_count | table3_count | table4_count | table4_size
---------------------------------------------------------------
test |      3       |      1       |      2       |    1224

任何帮助将不胜感激

2 个答案:

答案 0 :(得分:1)

您需要使用DISTINCT,但您还需要计算ID,而不是外键:

SELECT
    table1.name,
    COUNT(DISTINCT table2.id) AS table2_count,
    COUNT(DISTINCT table3.id) AS table3_count,
    COUNT(DISTINCT table4.id) AS table4_count,
    SUM(table4.size) AS table4_size
FROM table1
LEFT JOIN table2 ON table1.id = table2.table1_id
LEFT JOIN table3 ON table2.id = table3.table2_id
LEFT JOIN table4 ON table3.id = table4.table3_id
WHERE table1.id = 1

这是fiddle

说明DISTINCT关键字会消除所有重复值,从而产生唯一值列表。

如果您在没有COUNT()SUM()的情况下运行查询,则会获得:

name  table1_id  table2_id  table3_id  size
test  1          1          1          1024 
test  1          1          1          200 
test  1          (null)     (null)     (null) 
test  1          (null)     (null)     (null) 

因此,如果您添加COUNT()SUM(),您显然会得到:

name  table1_id  table2_id  table3_id  size
test  4          2          2          1224 

但是,在您的查询中使用DISTINCT将无济于事,因为您可以清楚地看到重复的值,这将导致:

name  table1_id  table2_id  table3_id  size
test  1          1          1          1224 

现在,如果您在没有COUNT()SUM()的情况下运行我的查询,则会得到:

name  table1_id  table2_id  table3_id  size
test  1          1          1          1024 
test  1          1          2          200 
test  2          (null)     (null)     (null) 
test  3          (null)     (null)     (null) 

如果您添加COUNT()SUM(),则会得到与查询完全相同的结果:

name  table1_id  table2_id  table3_id  size
test  4          2          2          1224 

但是,因为这次您有不同的值(即并非所有值都是1),所以现在如果您使用DISTINCT计算唯一值,则得到:

name  table1_id  table2_id  table3_id  size
test  3          1          2          1224 

答案 1 :(得分:0)

你能这样试试吗

SELECT  table1.name,
    (SELECT COUNT(table2.table1_id) WHERE  table1.id = table2.table1_id ) AS table2_count,
    (SELECT COUNT(table3.table2_id) WHERE  table2.id = table3.table1_id ) AS table3_count,
    (SELECT COUNT(table4.table3_id) WHERE  table3.id = table4.table1_id ) AS table4_count,
    (SELECT SUM(table4.size) WHERE  table3.id = table4.table1_id ) AS table4_size
    FROM
    table1
    WHERE
    table1.id = 1