SQL全外连接多个表

时间:2013-07-15 19:46:30

标签: mysql sql database join

首先我有4张桌子

Table0, Columns: num, desc
Table1, Columns: num, qty1
Table2, Columns: num, qty2
Table3, Columns: num, qty3
Table4, Columns: num, qty4

(并非所有num都有qty1或qty2或qty3或qty4中的值,因此我需要一个完整的连接) 和我的疑问:

SELECT Table0.num, SUM(Table1.qty1 ), SUM(Table2.qty2 ), SUM(Table3.qty3 ), SUM(Table4.qty4)
FROM Table0
FULL OUTER JOIN Table1 ON Table0.num = Table1.num
FULL OUTER JOIN Table2 ON Table0.num = Table2.num
FULL OUTER JOIN Table3 ON Table0.num = Table3.num
FULL OUTER JOIN Table4 ON Table0.num = Table4.num
GROUP BY Table0.num

不知何故,它只返回了一行数据:

num | qty1 | qty2 | qty3 | qty4 |
---------------------------------
    | 100  | 20   |  77  |  969 |

但我期待像

那样的例子

http://www.w3schools.com/sql/sql_join_full.asp

像:

num | qty1 | qty2 | qty3 | qty4 |
---------------------------------
1   |   0  |  2   |  3   |   2  |
2   |   1  |  0   |  0   |   0  |
3   |   7  |  0   |  9   |   0  |
4   |   0  |  0   |  0   |  10  |
5   |   0  |  0   |  7   |   0  |
6   |   8  |  2   |  9   |   3  |
7   |   0  |  1   |  0   |   0  |

(我不知道这解决了) 但是,通过将所有表格更改为:

,我得到了与上面结果相似的结果
Table1, Columns: num, qty1, qty2, qty3, qty4
Table2, Columns: num, qty2, qty1, qty3, qty4
Table3, Columns: num, qty3, qty1, qty2, qty4 
Table4, Columns: num, qty4, qty1, qty2, qty3 

3 个答案:

答案 0 :(得分:2)

您需要执行以下两项操作之一(并且这两项都假设Table0包含num的所有实例) -

  1. 如果所有行都已经为“叶子”表(1 - 4)求和了,那么一个简单的LEFT JOIN(在选择中带有COALESCE()就足够了 - 你不要甚至需要GROUP BY

  2. 如果你需要总和的行,你需要在连接之前将它们加起来,因为否则不同表中每个num有多行会导致结果乘法

  3. 这样的事情:

    SELECT Table0.num, COALESCE(Table1.qty, 0), COALESCE(Table2.qty, 0), 
                       COALESCE(Table3.qty, 0), COALESCE(Table4.qty, 0)
    FROM Table0
    LEFT JOIN (SELECT num, SUM(qty1) as qty
               FROM Table1
               GROUP BY num) Table1
    ON Table1.num = Table0.num
    LEFT JOIN (SELECT num, SUM(qty2) as qty
               FROM Table2
               GROUP BY num) Table2
    ON Table2.num = Table0.num
    LEFT JOIN (SELECT num, SUM(qty3) as qty
               FROM Table3
               GROUP BY num) Table3
    ON Table3.num = Table0.num
    LEFT JOIN (SELECT num, SUM(qty4) as qty
               FROM Table4
               GROUP BY num) Table4
    ON Table4.num = Table0.num
    

    (工作SQLFiddle example

答案 1 :(得分:1)

每个表中的num列之间没有匹配项,因此您将获得外部记录。当匹配键上没有匹配时,记录显示为该列为空。

完全外连接的方式,Table0.num需要存在于所有其他表中。即如果num == 1仅在Table0和Table1中,但不在Table2和3中,那么它将不会在所有4上匹配,因此是一个空数。

您可能想要的更像是

SELECT Table0.num, 
     (Select SUM(Table1.qty1 ) From Table1 Where Table1.num = Table0.num) as one,
     (Select SUM(Table2.qty1 ) From Table2 Where Table2.num = Table0.num) as two,
     ...
From Table0

我的语法可能有点偏差,可能会有更有效的方法。但一般的想法是你为每个关系做一个子查询,因为它们是独立的。

答案 2 :(得分:-2)

解决方法。您可以添加一个附加表来加入表格。例如:

Tablex;列:tablex_id (PK, AI) (新创建的表)

Table0;列:tablex_id, num, desc (添加新字段' tablex_id')

Table1;列:tablex_id, num, qty1 (添加新字段' tablex_id')

Table2;列:tablex_id, num, qty2 (添加新字段' tablex_id')

Table3;列:tablex_id, num, qty3 (添加新字段' tablex_id')

Table4;列:tablex_id, num, qty4添加新字段' tablex_id')

每次向Table0Table1Table2Table3Table4添加记录时,都应首先将记录添加到Tablex然后得到tablex_id并将其放在你的表格中。

如果您想加入他们,请执行以下操作:

select t0.desc, t1.qty, t2.qty, t3.qty, t4.qty
from Tablex tx
left join Table0 t0
  on tx.tablex_id = t0.tablex_id
left join Table0 t1
  on tx.tablex_id = t1.tablex_id
left join Table2 t2
  on tx.tablex_id = t2.tablex_id
left join Table3 t3
  on tx.tablex_id = t3.tablex_id
left join Table4 t4
 on tx.tablex_id = t4.tablex_id