Dear All:
     大家好!最近使用exists时遇到一个问题,请大家不甚赐教。
先附上测试数据:
create table test1
(col1 varchar2(10),
col4 number);create table test2
(col1 varchar2(10),
col2 number);insert into TEST1 (COL1, COL4)
values ('A', 1);
insert into TEST1 (COL1, COL4)
values ('B', 2);
insert into TEST1 (COL1, COL4)
values ('C', 3);
insert into TEST1 (COL1, COL4)
values ('D', 4);insert into TEST2 (COL1, COL2)
values ('A', 1);
insert into TEST2 (COL1, COL2)
values ('D', 2);
insert into TEST2 (COL1, COL2)
values ('E', 3);
insert into TEST2 (COL1, COL2)
values ('F', 4);
insert into TEST2 (COL1, COL2)
values ('G', 5);
insert into TEST2 (COL1, COL2)
values ('H', 6);
commit;对于下面的select语句,我困惑了...
select a.* from test1 a inner join test2 b 
on a.col1 = b.col1
and exists(select 1 from test2 c where c.col1 = a.col1
and c.col2>b.col2);我觉得查询结果应该是:
col1   col4
A 1
D 4但实际是:没有任何符合条件记录集。exists(select 1 from test2 c where c.col1 = a.col1
and c.col2>b.col2)
这段c.col2>b.col2 难道返回的是false? test2自身进行关联,test2中应该有记录满足test2.col2>test2.col2的啊?小弟愚钝,请大家指点迷津!谢谢

解决方案 »

  1.   

    select * from test1 m where exists(select 1 from test2 n where n.col1 = m.col1 and n.col2 <= m.col4)/*
    col1       col4        
    ---------- ----------- 
    A          1
    D          4(所影响的行数为 2 行)
    */
      

  2.   

    直接这样即可.
    select m.* from test1 m , test2 n where n.col1 = m.col1 and n.col2 <= m.col4/*
    col1       col4        
    ---------- ----------- 
    A          1
    D          4(所影响的行数为 2 行)
    */
      

  3.   

    既然都是关联test2 
    有inner join 就不要exists了
    要exists就不要inner join
      

  4.   

    楼主是否能够理解下面的结果?
    SQL> select a.*
      2  from test1 a inner join test2 b on a.col1 = b.col1;COL1             COL4
    ---------- ----------
    A                   1
    D                   4SQL>
    然后再看是否有记录符合楼主的条件。
      

  5.   

    同意3楼兄弟的意见,但是这句sql是其他同仁写的。我就觉的有问题。
    最后祝大家新年快乐。