SELECT子句与三个表一起

时间:2014-05-01 05:42:04

标签: sql oracle

示例,有这3个表

DEPARTMENT

//DEPARTMENT
DNAME       BUDGET      CHAIRMAN
----------------------------------
RESEARCH     2000         JOHN
   IT        3000          MAY
 SCIENCE     5000         KING

PROJECT

//PROJECT
P#     TITLE     BUDGET    STARTD          ENDD
----------------------------------------------------
1      COMPUTING   200     13-10-1993      13-11-1993
2       CODING     300     15-10-1993      15-12-1993
3      SEARCHING   188     07-07-1995      08-08-1995

DP< - 假设已完成约束引用,只是没有在此处显示编码

//DP
DNAME        P#
----------------
RESEARCH      1
  IT          2
RESEARCH      3

当我运行此声明时

SELECT d.DNAME,d.BUDGET,d.CHAIRMAN,p.TITLE,p.BUDGET,p.STARTD,p.ENDD 
     FROM DEPARTMENT d INNER JOIN PROJECT p 
     INNER JOIN DP dp ON DP.P#=p.P#;

但显示一些错误。想得到这样的输出: 仅显示项目表中涉及的那些

DNAME    BUDGET   CHAIRMAN     TITLE      BUDGET     STARTD        ENDD
---------------------------------------------------------------------------
RESEARCH  2000     JOHN       COMPUTING    200       13-10-1993  13-11-1993
RESEARCH  2000     JOHN       SEARCHING    188       07-07-1995  08-08-1995
   IT     3000      MAY         CODING     300       15-10-1993  15-12-1993

1 个答案:

答案 0 :(得分:1)

将查询更改为:

SELECT d.DNAME,d.BUDGET,d.CHAIRMAN,p.TITLE,p.BUDGET,p.STARTD,p.ENDD 
     FROM DEPARTMENT d INNER JOIN dp on dp.dname=d.dname
     INNER JOIN project p ON DP.P=p.P;

fiddle