SQL连接没有数据丢失

时间:2014-02-13 20:21:53

标签: sql oracle

我想要一个自然加入2个表的选择。如果在表B的任何行中没有使用该行的主键,那么将表A与表B连接时,新的临时表C不包含表A的行。我理解为什么会发生这种情况,但我也希望行也在表C中。

实施例

select deptno, dname, loc, count(deptno) empcount, round(avg(sal),2) avgsal
from dept natural join emp
group by deptno, dname, loc

结果:

    DEPTNO DNAME          LOC             EMPCOUNT     AVGSAL
---------- -------------- ------------- ---------- ----------
        20 RESEARCH       DALLAS                 5       2175 
        10 ACCOUNTING     NEW YORK               3    2916.67 
        30 SALES          CHICAGO                6    1566.67 

我想要的是什么:

    DEPTNO DNAME          LOC             EMPCOUNT     AVGSAL
---------- -------------- ------------- ---------- ----------
        20 RESEARCH       DALLAS                 5       2175 
        10 ACCOUNTING     NEW YORK               3    2916.67 
        30 SALES          CHICAGO                6    1566.67 
        40 OPERATIONS     BOSTON                 0          0

表部门(A):

    DEPTNO DNAME          LOC         
---------- -------------- -------------
        10 ACCOUNTING     NEW YORK      
        20 RESEARCH       DALLAS        
        30 SALES          CHICAGO       
        40 OPERATIONS     BOSTON        

表emp(B):

     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
      7369 SMITH      CLERK           7902 17-DEC-80        800                    20 
      7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300         30 
      7521 WARD       SALESMAN        7698 22-FEB-81       1250        500         30 
      7566 JONES      MANAGER         7839 02-APR-81       2975                    20 
      7654 MARTIN     SALESMAN        7698 28-SEP-81       1250       1400         30 
      7698 BLAKE      MANAGER         7839 01-MAY-81       2850                    30 
      7782 CLARK      MANAGER         7839 09-JUN-81       2450                    10 
      7788 SCOTT      ANALYST         7566 19-APR-87       3000                    20 
      7839 KING       PRESIDENT            17-NOV-81       5000                    10 
      7844 TURNER     SALESMAN        7698 08-SEP-81       1500          0         30 
      7876 ADAMS      CLERK           7788 23-MAY-87       1100                    20 
      7900 JAMES      CLERK           7698 03-DEC-81        950                    30 
      7902 FORD       ANALYST         7566 03-DEC-81       3000                    20 
      7934 MILLER     CLERK           7782 23-JAN-82       1300                    10 

4 个答案:

答案 0 :(得分:2)

使用left join代替natural join

select deptno, dname, loc, count(deptno) empcount, round(coalesce(avg(sal), 0),2) avgsal
from dept left join emp
group by deptno, dname, loc

答案 1 :(得分:2)

select deptno, dname, loc, count(deptno) empcount, round(avg(sal),2) avgsal
from dept 
LEFT join emp
group by deptno, dname, loc

这是你在看什么?检查输出......

答案 2 :(得分:2)

根据http://docs.oracle.com/javadb/10.8.3.0/ref/rrefsqljnaturaljoin.html,您可以像这样进行外部自然连接:

The following example is similar to the one above, but it also preserves
unmatched rows from the first (left) table:

SELECT * FROM COUNTRIES NATURAL LEFT JOIN CITIES

所以你会这样做:

select deptno, dname, loc, count(deptno) empcount, round(avg(sal),2) avgsal
from dept natural left join emp
group by deptno, dname, loc

答案 3 :(得分:0)

非常感谢,这是我正在寻找的外部联接。 所以这是sql命令,它提供了我正在寻找的结果:

SQL99-语法:

select deptno, dname, loc, nvl(ecount,0) empcount, nvl(round(avg(sal),2),0) avgsal
from dept left join emp using(deptno)
  left join (select deptno, count(deptno) ecount
             from emp
             group by deptno) using(deptno)
group by deptno, dname, loc, ecount

标准SQL语法:

select d.deptno, d.dname, d.loc, nvl(n.ecount,0) empcount, nvl(round(avg(e.sal),2),0) avgsal
from dept d, emp e, (select deptno, count(deptno) ecount
                     from emp
                     group by deptno) n
where d.deptno = e.deptno (+)
and n.deptno (+) = e.deptno
group by d.deptno, d.dname, d.loc, n.ecount

如果部门中没有员工,则列empcount为0。在其他解决方案中它是1,因为在临时表中的外连接之后,每个部门都有一行。