SELECT子句使用3个不同的表

时间:2014-05-01 03:56:37

标签: sql oracle

拥有这3张桌子

DEPARTMENT

 //DEPARTMENT
 DNAME
 -----------
 RESEARCH
    IT
  SCIENCE

PROJECT

//PROJECT
P#      TITLE
----------------
1      COMPUTING
2       CODING
3       SEARCHING

DP< - 假设完成了约束引用,我没有在这里显示代码

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

使用此声明时

SELECT d.DNAME,P.TITLE FROM DEPARTMENT d 
      INNER JOIN PROJECT 
      INNER JOIN DP ON d.DNAME=DP.DNAME AND P.P#=DP.P#;

为了获得这样的输出我应该如何改变,只显示具有项目的DNAME

DNAME         TITLES
---------------------
RESEARCH     COMPUTING
RESEARCH     SEARCHING
   IT         CODING

3 个答案:

答案 0 :(得分:1)

尝试此查询

SELECT d.DNAME,P.TITLE from DEPARTMENT d
  INNER JOIN DP ON d.DNAME=DP.DNAME
  INNER JOIN PROJECT P ON P.P#=DP.P#
order by d.DNAME DESC, P.TITLE ASC

答案 1 :(得分:0)

DEPARTMENT表格中已存在DP数据。所以使用它。

select DNAME, TITLE
from DP d, PROJECT p
where d.p# = p.p#
order by 1

答案 2 :(得分:0)

您也可以尝试:

MYSQL语法:

SELECT d.DNAME,P.TITLE from DEPARTMENT d
  INNER JOIN PROJECT P
  INNER JOIN DP ON d.DNAME=DP.DNAME AND P.P#=DP.P#
order by d.DNAME DESC, P.TITLE ASC

Running fiddle

SQL语法

SELECT d.dname,p.title from DEPARTMENT d,PROJECT P
  ,DP where d.DNAME=DP.DNAME and
  P.P#=DP.P#
order by d.dname desc, P.title asc

Fiddle

我不知道为什么在最后一个内连接中有三个表和on条件的内连接不能与MYSQL一起使用。很高兴你找到了解决方案。