【问题标题】:Join three tables without union加入三个没有联合的表
【发布时间】:2015-01-18 20:22:54
【问题描述】:

我有这张桌子

Main_Table

Date    Center      Response    Indicator       IDSurvey
--------------------------------------------------------
11-14   C1          10          LOP             432
11-15   C2          20          IUY             235
11-16   C1          35          LOP             125
11-17   C3          65          OIU             548
11-18   C3          45          OIU             693
11-19   C2          68          RES             236

我需要将这个表与两个互斥的表连接起来

Table_A
----------------------------------
Center      Group       Indicator
C1          GroupA1     LOP
C2          GroupA2     IUY
C3          GroupA3     OIU

Table_B
--------------------------------
Center      Group       IDSurvey
C1          GroupB1     236

与 union 的连接将带来我主表的六条记录。没有联合有没有办法做到这一点?

这是查询

select da.Date, da.Center, da.Response, da.Indicator, da.IDSurvey 
from Main_Table da
join Table_A ca 
on da.Indicator = ca.Indicator and ca.Center=da.Center
union
select da.Date, da.Center, da.Response, da.Indicator, da.IDSurvey 
from Main_Table da
join Table_B ca 
on ca.Center=da.Center and ca.IDSurvey=da.IDSurvey

【问题讨论】:

  • 为什么不想使用union
  • @Supersnake 原因很多,其中两个:好奇心和寻求最佳解决方案的目标
  • 最好的解决方案可能是union 查询。也许使用union all
  • 因为您不使用 Table_A 或 Table_B 中的任何列,显然您仍然希望返回 Main 中的所有记录。那么您认为 Table_A 和 Table_B 应该扮演什么角色呢?为什么不从 Main 中选择所有行?

标签: sql oracle join


【解决方案1】:

为什么不直接使用JOINTableB 以及TableA 之类的方法

select da.Date, da.Center, da.Response, da.Indicator, da.IDSurvey 
from Main_Table da
left join Table_A ca 
on da.Indicator = ca.Indicator and ca.Center = da.Center
left join Table_B ba 
on ba.Center = da.Center and ba.IDSurvey = da.IDSurvey;

【讨论】:

  • 这对我不起作用,我在 oracle 中,这没有给我任何记录;我认为原因是因为首先使用 table_a 进行连接,然后尝试使用 table_b 连接结果
  • @NatyBizz,在这种情况下,请考虑改用LEFT JOIN。请参阅答案中的编辑。
【解决方案2】:

您只是从主表中选择行。我认为您应该改用inexists

select da.Date, da.Center, da.Response, da.Indicator, da.IDSurvey 
from Main_Table da
where exists (select 1 from Table_A ca where da.Indicator = ca.Indicator and ca.Center = da.Center) or
      exists (select 1 from Table_B ca where ca.Center = da.Center and ca.IDSurvey = da.IDSurvey)

您的union 也在删除重复项。通过使用exists,您既不需要union,也不需要重复消除。而且,这可以很好地利用Table_ATable_B 上的索引。

【讨论】:

    【解决方案3】:

    要获得与使用联合的 2 个连接完全相同的结果,您需要使用以下 WHERE 子句从主表中排除没有任何详细信息的记录:

    select da.Date, da.Center, da.Response, da.Indicator, da.IDSurvey 
    from Main_Table da
    left join Table_A ca 
    on da.Indicator = ca.Indicator and ca.Center = da.Center
    left join Table_B ba 
    on ba.Center = da.Center and ba.IDSurvey = da.IDSurvey
    WHERE (ca.rowid is not null or ba.rowid is not null);
    

    顺便说一句,union 也隐式执行 group by。因此,如果您真的想删除重复项,那么您还需要添加 group by 所有列。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-07
      • 2016-05-21
      • 2018-03-29
      • 2016-02-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多