关于多个子查询,其中一个为空,那所有数据都没有,该如何解决该问题select * from 
(select f1 from t1 where ...) a1,
(select f1 from t1 where ...) a2,
(select f1 from t2 where ...) a3,
.....................如果a1没有记录,即使其他子查询也有数,那主查询也没数,我想不论子查询是否有没有数,只要别的子查询有记录,就可以获取,该如何解决呢?

解决方案 »

  1.   

    这个需要确认一张主表,意思就是说一定会有数据,其他的可能会没有,用外联接关联就行a.id=b.id(+) and a.id=c.id(+)
    如果说都有可能没有的话,就用多个union 两两关联查吧。
      

  2.   

    表t1
    f1   rq
    10  2016-10-13
    f2  rq
    20 2016-10-11select a1.f1,a1.f1-a2.f1  from 
     (select f1 from t1 where rq='2016-10-13') a1,
     (select f1 from t1 where rq='2016-10-12') a212号这天没数据,结果主查询就没记录
    我想实现即使12号没数据,但是13号的数据仍然可以获取,即得到10
      

  3.   

    用LEFT JOIN 实现SQL> select e.* from emp e where  e.empno=7369;     EMPNO ENAME      JOB              MGR HIREDATE              SAL       COMM     DEPTNO
    ---------- ---------- --------- ---------- -------------- ---------- ---------- ----------
          7369 SMITH      CLERK           7902 17-12月-80            800                    20SQL> select e.* from emp e where  e.empno=7970;未选定行SQL> select e1.* from (select e.* from emp e where  e.empno=7369) e1 left join
      2  (select e.* from emp e where  e.empno=7970) e2 on e1.empno=e2.empno;     EMPNO ENAME      JOB              MGR HIREDATE              SAL       COMM     DEPTNO
    ---------- ---------- --------- ---------- -------------- ---------- ---------- ----------
          7369 SMITH      CLERK           7902 17-12月-80            800                    20
      

  4.   

    外连接with t1 as 
    (select 10 f1,'2016-10-13' rq from dual
    union all
    select 20 f1,'2016-10-11' rq from dual
    )
    select a1.f1,a1.f1-a2.f1  from 
     (select f1 from t1 where rq='2016-10-13') a1 
     full join 
     (select f1 from t1 where rq='2016-10-12') a2
     on 1=1
      

  5.   

    建议度娘 oracle + 左连接(外连接)。
    再参照楼上各位SQL,理解左连接后
    请参考
    http://bbs.csdn.net/topics/390152319
      

  6.   

    select f1 from t1 where ..  union select f2 from t2 where ... union select f3 from t3 where ..