1

数据:我写了两个查询,一个是 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  

为什么 ?

4

2 回答 2

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;

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

于 2012-04-18T20:00:13.280 回答
3

where 子句fu.id_source_cur = 'A'与其他子句不同。fu.id_target_cur = 'A'

于 2012-04-18T20:00:17.737 回答