我想得到所有在部门30的工作,包括位置

时间:2011-09-15 03:26:56

标签: sql oracle sqlplus

我想获得所有在部门30中的工作,包括位置

 SQL> select deptno,job  from emp where deptno =30 (select loc from dept);
 select deptno,job  from emp where deptno =30 (select loc from dept)
                                         *
 ERROR at line 1:
 ORA-00933: SQL command not properly ended


 SQL>

表emp

    SQL> select empno, ename, job, hiredate, deptno from emp;

 EMPNO ENAME      JOB       HIREDATE      DEPTNO
 ---------- ---------- --------- --------- ----------
  7839 KING       PRESIDENT 17-NOV-81         10
  7698 BLAKE      MANAGER   01-MAY-81         30
  7782 CLARK      MANAGER   09-JUN-81         10
  7566 JONES      MANAGER   02-APR-81         20
  7654 MARTIN     SALESMAN  28-SEP-81         30
  7499 ALLEN      SALESMAN  20-FEB-81         30
  7844 TURNER     SALESMAN  08-SEP-81         30
  7900 JAMES      CLERK     03-DEC-81         30
  7521 WARD       SALESMAN  22-FEB-81         30
  7902 FORD       ANALYST   03-DEC-81         20
  7369 SMITH      CLERK     17-DEC-80         20

 EMPNO ENAME      JOB       HIREDATE      DEPTNO
 ---------- ---------- --------- --------- ----------
  7788 SCOTT      ANALYST   09-DEC-82         20
  7876 ADAMS      CLERK     12-JAN-83         20
  7934 MILLER     CLERK     23-JAN-82         10

  14 rows selected.

表部门

  SQL> select * from dept;

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

  SQL>

2 个答案:

答案 0 :(得分:3)

您需要加入两个表

SELECT deptno,
       e.job,
       d.loc
  FROM emp e 
       JOIN dept d USING (deptno)
 WHERE deptno = 30

答案 1 :(得分:0)

SELECT empno, ename, job, hiredate, emp.deptno, location FROM emp
  LEFT JOIN dept ON emp.deptno = dept.deptno
  WHERE emp.deptno = 30