【问题标题】:SQL/Oracle Server, Full Outer Join and 3 or more TablesSQL/Oracle 服务器,完全外连接和 3 个或更多表
【发布时间】:2011-01-01 15:14:35
【问题描述】:

我遇到了一个小问题。

我在 Sql 服务器中使用完全外连接 - 到目前为止它有效 但现在我添加了一个表格,但它不能正常工作。

这是我的代码:

SELECT *
FROM Table1 h , (db1..Table2 s FULL OUTER JOIN  db1..Table3 k
on k.attributT3_1 = s.attributT2_1 
    and left(k.attributT3_2,4) = year(s.attributT2_2)
    and substring(k.attributT3_2,6,1) = s.attributT2_2
    and (case when k.attributT3_3 = 0 and k.attributT3_4 = 11 then 10 
        when k.attributT3_3 = 0 and k.attributT3_4 = 14 then 40
        when k.attributT3_3 = 0 and k.attributT3_4 = 16 then 60 
        when k.attributT3_3 = 0 and k.attributT3_4 = 90 then 10 
        when k.attributT3_3 = 1 and k.attributT3_4 = 11 then 11 
        when k.attributT3_3 = 2 and k.attributT3_4 = 11 then 12 
        when k.attributT3_3 = 4 and k.attributT3_4 = 11 then 14 
        when k.attributT3_3 = 7 and k.attributT3_4 = 11 then 17 
        else k.attributT3_3 end) = s.attributT2_3) 
where h.attributT1_1 = k.attributT3_1 
and s.attributT3_1 = '  260585'
and h.attributT1_2 = 055

这是我的SQL,可能有点混乱^^

我的问题是 FULL OUTER JOIN 不起作用,因为我添加了 Table1,因为我需要一些信息,现在 FULL OUTER JOIN 像 LEFT OUTER JOIN 一样工作。

我也可能需要在这个 FULL OUTER JOIN 中添加一到两个表。

有没有人知道我怎样才能让它工作? (是的,我知道我可以使用 2 个左外连接和一个联合并使其工作,但这不是我想要做的)

顺便说一句,脚本也可以在 oracle 上运行:D

输出应该是这样的: http://img402.imageshack.us/img402/4618/bildwq.jpg

列 1 = 表 1 列 2 = 表 2 Column3 = Table3

表中不存在空值

我的脚本做同样的事情,除了你不能看到第 2 列中的行带有空值

【问题讨论】:

  • 我发现您对问题的解释令人困惑。你能展示你想要的输出与你得到的结果吗?

标签: sql-server oracle


【解决方案1】:

WHERE 子句限制了可以返回的结果:

where h.attributT1_1 = k.attributT3_1 
and s.attributT3_1 = '  260585'
and h.attributT1_2 = 055

这意味着仅显示表 H、K 和 S 贡献了一行(非空值)的结果。实际上,您已从联接中删除了 OUTER。尝试将这些条件移动到连接定义中。

一个例子来说明我的意思:

SQL> create table t1 (id integer, text varchar2(10));
SQL> create table t2 (id integer, text varchar2(10));
SQL> insert into t1 values (1, 'text');
SQL> insert into t1 values (2, 'text');
SQL> insert into t2 values (2, 'text');
SQL> insert into t2 values (3, 'text');
SQL> commit;

完全外连接:

SQL> select * from t1 full outer join t2 on t1.id = t2.id;

        ID TEXT               ID TEXT
---------- ---------- ---------- ----------
         2 text                2 text
                               3 text
         1 text

现在在 WHERE 子句中使用过滤器(注意 2 条记录消失,就像“完整外部”不存在一样):

  1* select * from t1 full outer join t2 on t1.id = t2.id
  2* where t1.text='text'
  3  and t2.text='text';

        ID TEXT               ID TEXT
---------- ---------- ---------- ----------
         2 text                2 text

现在将过滤器移到联接中:

SQL> select * from t1 full outer join t2 on t1.id = t2.id
  2                                      and t1.text='text'
  3                                      and t2.text='text';

        ID TEXT               ID TEXT
---------- ---------- ---------- ----------
         2 text                2 text
                               3 text
         1 text             

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-01
    • 2015-06-02
    • 2014-08-07
    • 1970-01-01
    • 2012-01-06
    • 1970-01-01
    相关资源
    最近更新 更多