WITH Query和SELECT Query有什么区别?

时间:2012-04-18 19:46:42

标签: sql oracle

数据:我写了两个查询,一个是WITH而另一个是SELECT,然后自己加入下面的表,但两个查询都返回不同的结果,为什么会发生?

表格名称为 test_cur

    ID_SOURCE_CUR   ID_TARGET_CUR
 -------------  --------------
    A               B
    B               C
    C               D
    D               E
    A               Z
    G               A
    K               A
    Q               A
    J               J
    K               K
    K               L
    L               K
    B               A
    Z               A

那么为什么下面的两个查询会返回不同的结果?

    SELECT *
      FROM test_cur tu, test_cur fu
     WHERE tu.id_target_cur = 'A'
       AND fu.id_source_cur = 'A'
       AND tu.id_source_cur <> fu.id_target_cur;

returns 8 rows.

ID_SOURCE_CUR   ID_TARGET_CUR   ID_SOURCE_CUR_1 ID_TARGET_CUR_1
--------------  --------------   --------------  -------------- 
    G               A               A               B
    K               A               A               B
    Q               A               A               B
    Z               A               A               B
    G               A               A               Z
    K               A               A               Z
    Q               A               A               Z
    B               A               A               Z 

和 -

    WITH qry1 AS
         (SELECT *
            FROM test_cur)
    SELECT *
      FROM qry1 tu, qry1 fu
     WHERE tu.id_target_cur = 'A'
       AND fu.id_target_cur = 'A'
       AND tu.id_source_cur <> fu.id_target_cur;

    returns 25 rows.

ID_SOURCE_CUR   ID_TARGET_CUR   ID_SOURCE_CUR_1 ID_TARGET_CUR_1
--------------  --------------   --------------  -------------- 
        G               A               G               A
        G               A               K               A
        G               A               Q               A
        G               A               B               A
        G               A               Z               A
        K               A               G               A
        K               A               K               A
        K               A               Q               A
        K               A               B               A
        K               A               Z               A
        Q               A               G               A
        Q               A               K               A
        Q               A               Q               A
        Q               A               B               A
        Q               A               Z               A
        B               A               G               A
        B               A               K               A
        B               A               Q               A
        B               A               B               A
        B               A               Z               A
        Z               A               G               A
        Z               A               K               A
        Z               A               Q               A
        Z               A               B               A
        Z               A               Z               A  

为什么?

2 个答案:

答案 0 :(得分:5)

您的第二个查询不同,您有一个不同的WHERE子句。第一个WHERE是:

WHERE tu.id_target_cur = 'A'
       AND fu.id_source_cur = 'A'
       AND tu.id_source_cur <> fu.id_target_cur;

第二个是:

WHERE tu.id_target_cur = 'A'
       AND fu.id_target_cur = 'A' -- this line is different, it should be fu.id_source_cur = 'A'
       AND tu.id_source_cur <> fu.id_target_cur;

更改这些内容,两个查询的结果完全相同。

答案 1 :(得分:3)

where子句不同fu.id_source_cur = 'A'fu.id_target_cur = 'A'