A表表结构
create table A
(
  ids varchar2(32),
  names varchar2(32)
)
B表表结构同A一模一样.
初开化数据
insert into A values('1','1'); 
insert into A values('2','2'); 
insert into A values('3','3');insert into B values('1','A');
insert into B values('2','B');
insert into B values('3','C');
insert into B values('4','D'); select * from A a where exists (select ids from B where a.ids = '2');查询的结果我预期为A表整个表的值,但是我无意写错查询出的结果却是
2,2
如果我改成
select * from A a where exists (select ids from B where a.ids = '1');
结果是
1,1请高手解释一下,越详细越好

解决方案 »

  1.   

    select * from A a where exists (select ids from B where a.ids = '2'); 1
    首先你要理解这是一个关联子查询 因为在子查询中使用到了a.ids2
    语句执行的时候 遍历表A 拿每个ids去和'2'比较 
    当A表中的1,1和3,3两条去比时条件a.ids='2'为false 因此子查询select ids from B where a.ids = '2'不会返回任何结果 exists子句返回也就相当于false 这个时候外层select * from A不返回结果
    只有2,2这条记录符合条件 因此返回了2,2