顶尖。如何也输出NULL值?

时间:2019-03-10 15:29:44

标签: sql oracle oracle-apex

我有一个关于与apex中的交互式表相关的sql查询的问题

在这里,我规定了一个请求,在该请求中,我将打印数据库中所有现有的演出者,但是我只有所有字段都具有值且其中至少不显示NULL的条目

select artist.name as "Artist", country.name as "Country" , city.name as "City of foundation", label.name as "Label of records"  
from artist, country, city, label
where artist.country = country_id
and artist.city = city_id
and city.country = country_id
and artist.label = label_id

如何解决?

https://i.stack.imgur.com/ZRYzm.png

1 个答案:

答案 0 :(得分:2)

由于您没有提供测试用例(屏幕截图不够用-至少对我来说不是),所以我将尝试说明使用Scott的架构可能会发生什么。

有4个部门:注意部门40,没有人在其中工作:

SQL> select * from dept;

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

SQL> select * from emp where deptno = 40;

no rows selected

如果要显示所有4个部门和在其中工作的员工,则需要加入EMP和DEPT表。通过外部联接,您可以显示部门40(如我们所见,该部门没有员工):

SQL> select d.deptno, d.dname, e.ename
  2  from dept d left join emp e on e.deptno = d.deptno      --> outer join is here
  3  order by d.deptno;

    DEPTNO DNAME          ENAME
---------- -------------- ----------
        10 ACCOUNTING     CLARK
        10 ACCOUNTING     MILLER
        10 ACCOUNTING     KING
        20 RESEARCH       JONES
        20 RESEARCH       SMITH
        20 RESEARCH       SCOTT
        20 RESEARCH       FORD
        20 RESEARCH       ADAMS
        30 SALES          WARD
        30 SALES          TURNER
        30 SALES          ALLEN
        30 SALES          JAMES
        30 SALES          MARTIN
        30 SALES          BLAKE
        40 OPERATIONS                       --> this is what you're looking for

15 rows selected.

SQL>

使用旧的Oracle (+)外连接运算符,您将获得相同的结果。不过,您宁愿切换到现代联接并避免使用该运算符。

SQL> select d.deptno, d.dname, e.ename
  2  from dept d, emp e
  3  where d.deptno = e.deptno (+)        --> the old outer join operator
  4  order by d.deptno;

    DEPTNO DNAME          ENAME
---------- -------------- ----------
        10 ACCOUNTING     CLARK
        10 ACCOUNTING     MILLER
        10 ACCOUNTING     KING
        20 RESEARCH       JONES
        20 RESEARCH       SMITH
        20 RESEARCH       SCOTT
        20 RESEARCH       FORD
        20 RESEARCH       ADAMS
        30 SALES          WARD
        30 SALES          TURNER
        30 SALES          ALLEN
        30 SALES          JAMES
        30 SALES          MARTIN
        30 SALES          BLAKE
        40 OPERATIONS

15 rows selected.

SQL>