(+)加入子查询

时间:2018-04-19 10:59:37

标签: sql oracle

我想从一个存在的查询中改变一个条件。条件是这样的:

and (w.wthd_class (+) = 'A' or w.wthd_class (+) = 'B')

所以我想要的是不要让这个条件静态我不想从子查询中动态检索WTHD_CLASS值,但同时不要忽略right outer join (+)

and (w.wthd_class (+) IN ( SELECT WTHD_CLASS FROM myTABLE T WHERE T.ACCOUNT = B.ACCOUNT )

但是这种语法会出现此错误:

  

ORA-01799:列可能不是外部连接到子查询

有人可以帮忙吗?

感谢。

3 个答案:

答案 0 :(得分:3)

使用ANSI连接语法:

SELECT *
FROM   table1 b
       LEFT OUTER JOIN table2 w
       ON ( b.some_column = w.some_column )
       RIGHT OUTER JOIN mytable t
       ON (
             t.account    = b.account
         AND w.wthd_class = t.wthd_class
       );

答案 1 :(得分:1)

基于Scott的架构不是太聪明的例子,但我希望你能得到这个想法。

旧的Oracle外连接(+)运算符:

$usersAuthTokens = new UsersAuthTokens;
$usersAuthTokens->auth_token = $authToken;
$usersAuthTokens->validity = $time;
$usersAuthTokens->refresh_token = $newRefreshToken;
$usersAuthTokens->user_id = 0;  //error is here..because no record in users table right now
$usersAuthTokens->save();

当使用子查询进行外部连接时,它将无法工作:

SQL> SELECT e.deptno,
  2         e.ename,
  3         e.job,
  4         e.sal
  5    FROM emp e, dept d
  6   WHERE     e.deptno(+) = d.deptno
  7         AND ROWNUM < 5;

    DEPTNO ENAME      JOB              SAL
---------- ---------- --------- ----------
        20 SMITH      CLERK            800
        30 ALLEN      SALESMAN        1600
        30 WARD       SALESMAN        1250
        20 JONES      MANAGER         2975

因此,使用ANSI外连接:

SQL> SELECT e.deptno,
  2         e.ename,
  3         e.job,
  4         e.sal
  5    FROM emp e, dept d
  6   WHERE     e.deptno(+) = (SELECT d.deptno
  7                              FROM dept d)
  8         AND ROWNUM < 5;
       AND ROWNUM < 5
       *
ERROR at line 8:
ORA-01799: a column may not be outer-joined to a subquery

最后,一个“普通”外连接,没有子查询:

SQL> SELECT e.deptno,
  2         e.ename,
  3         e.job,
  4         e.sal
  5    FROM emp e
  6         RIGHT JOIN (SELECT d.deptno
  7                       FROM dept d) x
  8            ON x.deptno = e.deptno
  9   WHERE ROWNUM < 5;

    DEPTNO ENAME      JOB              SAL
---------- ---------- --------- ----------
        20 SMITH      CLERK            800
        30 ALLEN      SALESMAN        1600
        30 WARD       SALESMAN        1250
        20 JONES      MANAGER         2975

SQL>

答案 2 :(得分:1)

select *
 from table1 a,
     (select * from table2 b
      where b.column1 = 'somevalue') x
where a.column1 (+) = x.column1