SQL A:
select * from tableA
where exists (select * from tableA a, tableB b where a.pk_a=b.pk_a and tableA.pk_x=a.pk_x)SQL B:
select * from tableA 
where exists (select * from tableA a inner join tableB b on a.pk_a=b.pk_a and tableA.pk_x=a.pk_x)在Oracle10g中执行上述两段语句,发现SQL A能够正常执行,但执行SQL B时,却报找不到字段tableA.pk_x。
请问,如何解释这一现象?另,上述两段代码在SQL Server中均能够正常执行。

解决方案 »

  1.   

    tableA 换成别名 a 试试
      

  2.   


    select * from tableA c 
    where exists (select * from tableA a inner join tableB b on a.pk_a=b.pk_a where tableA.pk_x=a.pk_x);
      

  3.   

    tableA.pk_x=a.pk_x这个条件多余的啊
      

  4.   

    给外层的tableA 加个别名  然后再试试。。
      

  5.   

    你tableA 和tableB 是通过a.pk_a=b.pk_a连接的,要跟外层的tableA 连接就不能再用在inner join on下了,而要另外取别名或加where条件。
      

  6.   

    晕,跟着gelyon跑到这里来,呵呵。
      

  7.   

    有那么复杂吗如果要比较tablea是否存在tablebselect * from tableA  a
    where exists (select * from tableB b where a.pk_a=b.pk_a );
    就行,或者直接内连接如果一定要在exists用2个表
    只能用where,才可以跟外表关联join 后的on和and条件 只能引用 join两边的表select * from tableA c  
    where exists (select * from tableA a inner join tableB b on a.pk_a=b.pk_a where c.pk_x=a.pk_x)
    ;
      

  8.   

    别名应该不关键,那个条件放在where子句中才是要紧。
    谢谢大家的帮助。
      

  9.   

    不符合语法.应该写成这样.on的条件只能是inner join table之间的关联条件.SQL> select * from emp
      2  where exists (select * from emp a inner join dept b on a.deptno=b.deptno where emp.deptno=a.deptno);
     
    EMPNO ENAME      JOB         MGR HIREDATE          SAL      COMM DEPTNO
    ----- ---------- --------- ----- ----------- --------- --------- ------
     7902 FORD       ANALYST    7566 1981-12-3     3000.00               20
     7876 ADAMS      CLERK      7788 1987-5-23     1100.00               20
     7788 SCOTT      ANALYST    7566 1987-4-19     3000.00               20
     7566 JONES      MANAGER    7839 1981-4-2      2975.00               20
     7369 SMITH      CLERK      1000 1980-12-17     800.10               20
     7900 JAMES      CLERK      7698 1981-12-3      950.00               30
     7844 TURNER     SALESMAN   7698 1981-9-8      1500.00      0.00     30
     7698 BLAKE      MANAGER    7839 1981-5-1      2850.00               30
     7654 MARTIN     SALESMAN   7698 1981-9-28     1250.00   1400.00     30
     7521 WARD       SALESMAN   7698 1981-2-22     1250.00    500.00     30
     7499 ALLEN      SALESMAN   7698 1981-2-20     1600.00    300.00     30
     7934 MILLER     CLERK      7782 1982-1-23     1300.00               10
     7839 KING       PRESIDENT       1981-11-17    5000.00               10
     7782 CLARK      MANAGER    7839 1981-6-9      2450.00               10
     
    14 rows selected