简体   繁体   中英

oracle joining multiple tables

I have two tables, one which look something like this

Position(posNum, posTitle, posSalary)

sample data:

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

and another table like this

SKILL(skill,skillDesc)

sample data:

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

which are referenced by a table like this

SKILLNEEDED(skillneededPosNum, skillneededSkill)

sample data:

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

is there any mistake in my query as it returns no rows found which should not be the case

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

我想在你的WHERE子句中应该有一个OR而不是AND

Your initial query fails because the entire WHERE is applied to each row. Clearly it cannot be true that a=1 AND a=2 so your query returns zero rows.

This solution is not necessarily the most elegant but I think it illustrates the principle. It uses the WITH clause to make a sub-query which can be referenced multiple times in the main query.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM