简体   繁体   English

将多个表合并在一起,正确的联接类型?

[英]Union Many Tables Together, Correct type of join?

I have more then 10 tables I would like to join together. 我有10张以上的桌子想加入。 Here is a basic example of what I need to do with 3 tables. 这是我需要处理3个表的一个基本示例。

Input Tables 输入表

Table 1
    ID, Time, Field1
    1, Mon, 5
    1, Tue, 6
    1, Wed, 7 

Table 2
    ID, Time, Field1
    1, Tue, 99
    1, Wed, 199
    1, Thu, 299

Table 3
    ID, Time, Field1
    1, Wed, 777
    1, Thu, 888
    1, Fri, 999

Desired Output Table 所需的输出表

Table Output
    ID, Time, T1.Field1, T2.Field1, T3.Field1
    1, Mon, 5, NULL, NULL
    1, Tue, 6, 99, NULL
    1, Wed, 7, 199, 777
    1, Thu, NULL, 299, 888
    1, Fri, NULL, NULL, 999

I have tried the following(with 9 tables), and it is not returning. 我尝试了以下(有9个表),并且没有返回。 Is this due to run time or have I done the wrong kind of join? 这是由于运行时间引起的还是我执行的联接类型错误?

Select 
    id, time, t1.field1, t2.field1, t3.field3
From
    table1 t1
FULL JOIN table2 t2 using (id, time)
FULL JOIN table3 t3 using (id, time);

You need a Left Outer join. 您需要一个左外部联接。 This will get all rows in t1, and any matching data in t2, t3 etc, but return NULLS if there is no matching data. 这将获取t1中的所有行,以及t2,t3等中的任何匹配数据,但是如果没有匹配数据,则返回NULLS。

Your query actually seems correct, or at least sane, bar an ambiguous column name and gaps that prevent rows from showing up when rows exist in table3 but not in table1 and table2. 您的查询实际上看起来是正确的,或者至少是明智的,排除了模棱两可的列名和空格,以防止当表3中存在行但表1和表2中不存在行时显示行。 So from the third table onward, you'll need to coalesce all over the place: 因此,从第三张表开始,您需要在整个地方合并:

Select 
    coalesce(t1.id, t2.id, t3.id, ...) as id,
    coalesce(t1.time, t2.time, t3.time, ...) as time,
    t1.field1 as field1,
    t2.field1 as field2, ...
From
    table1 t1
FULL JOIN table2 t2 using (id, time)
FULL JOIN table3 t3 on t3.id = coalesce(t1.id, t2.id)
                   and t3.time = coalesce(t1.time, t2.time)
FULL JOIN ...

If you want to fill the holes in addition, look into generate_series() -- and if you end up using the latter, left join and no coalesce will be fine. 如果您还想填补空缺,请查看generate_series() -如果最终使用后者,则left join并且没有合并效果会很好。

Also, seriously revisit that schema, and avoid joining the above mess with anything -- seq scans all over are guaranteed if you do. 此外,请认真地重新访问该架构,并避免将上面的混乱与任何事物混为一谈-如果您这样做,可以确保遍历seq扫描。

How about a pseudo table as the master being a complete list of unique id/time combinations and LEFT JOINing off that to ensure all other tables can be represented? 作为主表的伪表如何包含唯一的ID /时间组合的完整列表,并确保可以代表所有其他表,所以请左键联接怎么样?

SELECT m.id,m.time,t1.field,t2.field,t3.field
FROM (SELECT DISTINCT id,time FROM (
    SELECT id,time FROM t1 
    UNION 
    SELECT id,time FROM t2
    UNION
    SELECT id,time FROM t3)m) m
LEFT JOIN t1 ON t1.id=m.id AND t1.time=m.time
LEFT JOIN t2 ON t2.id=m.id AND t2.time=m.time
LEFT JOIN t3 ON t3.id=m.id AND t3.time=m.time

Efficiency may not be the best. 效率可能不是最好的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM