0

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

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';
4

2 回答 2

2

我想你的条款中应该有一个OR而不是一个。ANDWHERE

于 2013-11-01T03:20:23.110 回答
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';   
于 2013-11-01T04:08:43.803 回答