Oracle - Sql - 为什么不Q3,返回与Q2相同的结果?

时间:2010-08-20 18:41:49

标签: sql oracle

DROP TABLE table1;
DROP TABLE table2;

CREATE TABLE table1
(   col1  NUMBER,
    col2  NUMBER,
    col3  NUMBER,
    col4  NUMBER
);

CREATE TABLE table2
(   col1  NUMBER,
    col2  NUMBER,
    col3  NUMBER,
    col4  NUMBER
);

INSERT INTO TABLE1 VALUES (1,44,1,1);
INSERT INTO table1 VALUES (2,44,2,2);
INSERT INTO TABLE1 VALUES (3,44,3,3);

INSERT INTO table2  VALUES (1,44,11,11);
INSERT INTO TABLE2  VALUES (2,44,22,22);

-- Q1
SELECT t1.*, t2.* FROM table1 t1, table2 t2
WHERE t1.col1 = t2.col1(+);

-- Yields
-- col1 col2 col3 col4 col1_1   col2_1 col3_1 col4_1
      1   44    1    1      1       44     11     11
      2   44    2    2      2       44     22     22
      3   44    3    3      NULL   NULL  NULL   NULL


-- Q2
SELECT t1.*, t2.* FROM table1 t1, table2 t2
WHERE t1.col1 = t2.col1(+)
AND             t2.col1 IS NULL;

-- Yields
-- col1 col2 col3 col4 col1_1   col2_1 col3_1 col4_1
      3   44    3    3   NULL     NULL   NULL   NULL


-- Q3
SELECT t1.*, t2.* FROM table1 t1, table2 t2
WHERE t1.col2 = 44
  AND t2.col2 = 44 
  AND t1.col1 = t2.col1(+)
  AND           t2.col1 IS NULL;

-- Yields
-- col1 col2 col3 col4 col1_1   col2_1 col3_1 col4_1

-- No Rows.

COMMIT;

2 个答案:

答案 0 :(得分:2)

因为t2.col1 IS NULLt2.col2 = 44不会同时成立。

如果t2.col1 IS NULL,则表示联接不匹配,因此t2.col2也将是NULL

答案 1 :(得分:0)

t2.col2不可能是44,如果您没有从整个桌子的左连接中获得结果,则会在WHERE中进行检查!