且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

SQL查询以显示父表的记录以及其所有子表的记录

更新时间:2023-02-05 13:49:44

从我的角度来看,该任务并不像看起来那样简单.好的,我的SQL * Plus功能可能不如以前/应该的那样好.主要问题是如何使每个部门的EMP表标题重复(不,我不知道该怎么做).

From my point of view, that task isn't as simple as it looks. OK, my SQL*Plus capabilities probably aren't as good as they used to / should be. The main problem is how to make EMP table's headings repeat for each department (and no, I don't know how to do that).

我设法产生的最接近的结果是:

The closest result I managed to produce is this:

SQL> col deptno format 999999
SQL> col dname  format a10
SQL> col loc    format a10
SQL> set linesize 30
SQL> set recsep off
SQL> break on deptno on dname on loc
SQL>
SQL> select d.deptno, d.dname, d.loc, e.empno, e.ename
  2  from dept d left join emp e on e.deptno = d.deptno
  3  order by d.deptno;

 DEPTNO DNAME      LOC
------- ---------- ----------
     EMPNO ENAME
---------- ----------
     10 ACCOUNTING NEW YORK
      7839 KING

      7782 CLARK

      7934 MILLER
     20 RESEARCH   DALLAS
      7902 FORD

      7369 SMITH

      7566 JONES
     30 SALES      CHICAGO
      7900 JAMES

      7844 TURNER

      7654 MARTIN

      7521 WARD

      7499 ALLEN

      7698 BLAKE
     40 OPERATIONS BOSTON


13 rows selected.

一旦您,Enferno(或其他任何人)都能设法准确地获得所需的内容,我真的很想看看最终的代码.

Once you, Enferno (or anyone else) manages to get exactly what's been asked for, I'd really like to see the final code.