oracle加入多个表

时间:2013-11-01 03:15:10

标签: sql oracle jointable

我有两张桌子,一张看起来像这样的桌子

Position(posNum, posTitle, posSalary)

示例数据:

insert into position values ('P0009','Systems Engineers',5600);
insert into position values ('P0010','Senior Lecturer', 9000);
insert into position values ('P0011','Database Administrator',4500);

和另一个像这样的表

SKILL(skill,skillDesc)

示例数据:

insert into skill values ('SK009','Database Optimization');
insert into skill values ('SK010','Oracle XE 11g');

由像这样的表引用

SKILLNEEDED(skillneededPosNum, skillneededSkill)

示例数据:

insert into skillneeded values ('P0009','SK010',10);
insert into skillneeded values ('P0010','SK401',10);
insert into skillneeded values ('P0010','SK807',10);

我的查询中是否有任何错误,因为它没有找到找不到的行

SELECT p.posNum, p.posTitle, p.posOfferedBy
  FROM POSITION p
  JOIN SKILLNEEDED c
    ON p.posNum = c.skillneededPosNum
  JOIN SKILL s
    ON s.skill = c.skillneededSkill
 WHERE s.skillDesc = 'Oracle XE 11g' AND s.skillDesc = 'Database Design';

2 个答案:

答案 0 :(得分:2)

我认为OR条款中应该有AND而不是WHERE

答案 1 :(得分:0)

您的初始查询失败,因为整个WHERE应用于每一行。很明显,a=1 AND a=2不是这样,所以你的查询返回零行。

这个解决方案不一定是最优雅的,但我认为这说明了原则。它使用WITH子句创建一个子查询,可以在主查询中多次引用。

with skillz as
   ( select p.posNum, s.skillDesc
     from SKILLNEEDED c
          join SKILL s
              ON s.skill = c.skillneededSkill
          join POSITION p
              ON p.posNum = c.skillneededPosNum )
select p.*
from POSITION p
     join skillz s1 
          on s1.posNum = p.posNum 
     join skillz s2 
          on s2.posNum = p.posNum 
WHERE s1.skillDesc = 'Oracle XE 11g' 
AND s2.skillDesc = 'Database Design';