简体   繁体   English

加入或合并所有人以合并记录?

[英]Join or Union All To Combine Records?

I have 2 similar tables that contain campaign names. 我有2个类似的包含广告系列名称的表。 I know I can do an union all to combine the tables, but I was wondering if there was a way to do this using form of Join instead? 我知道我可以全部合并来合并表,但是我想知道是否有一种方法可以使用Join形式来代替? I want to create a table Z with campaign names for table A plus campaign names from table B (which are not in A). 我想用表A的活动名称以及表B中的活动名称(不在A中)创建表Z。 Can I do this with a join or is Union ALL the only way? 我可以通过联接执行此操作吗,还是Union ALL是唯一方法?

SELECT * INTO TABLEZ
FROM 
  (
    SELECT Column1, Column2, Column3.... FROM TABLEA 
    UNION ALL
    SELECT Column1, Column2, Column3.... FROM TABLEB
  )Q

UNION is the easier and correct way to do that. UNION是更简单,更正确的方法。 Purely for the exercise you can do it with a JOIN but it is a lot more complex, unreadable, and the perf will be way worse... 纯粹是为了练习,您可以使用JOIN进行操作,但是它要复杂得多,难以理解,并且性能会更差...

Here is how you would do this with a full outer join : 使用full outer join如下:

select distinct coalesce(a.campaign, b.campaign)
from b left outer join
     a
     on a.campaign = b.campaign;

The union / union all approach is totally reasonable. union / union all做法都是完全合理的。 I'm just offering this as a join solution that you seem to be alluding to in the question. 我只是提供这种作为一个join ,你似乎在这个问题被影射的解决方案。

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

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