3

我有一个数据库表,其中的人由姓名、工作和城市标识。我有第二个表,其中包含每个城市公司中每个工作的分层表示。

假设我在 people 表中有 3 个人:

[name(PK),title,city]
Jim, Salesman, Houston
Jane, Associate Marketer, Chicago
Bill, Cashier, New York

我在工作表中有数千个工作类型/位置组合,下面是其中的一个示例。您可以看到层次关系,因为 parent_title 是标题的外键:

[title,city,pay,parent_title]
Salesman, Houston, $50000, CEO
Cashier, Houston, $25000
CEO, USA, $1000000
Associate Marketer, Chicago, $75000
Senior Marketer, Chicago, $125000

......

我遇到的问题是我的 Person 表是一个复合键,所以我不知道如何构造start with我的查询部分,以便它从我指定的城市中的三个工作中的每一个开始。

我可以执行三个单独的查询来获得我想要的,但这不能很好地扩展。例如:

select * from jobs
start with city = (select city from people where name = 'Bill') and title = (select title from people where name = 'Bill')
connect by prior parent_title = title
UNION
select * from jobs
start with city = (select city from people where name = 'Jim') and title = (select title from people where name = 'Jim')
connect by prior parent_title = title
UNION
select * from jobs
start with city = (select city from people where name = 'Jane') and title = (select title from people where name = 'Jane')
connect by prior parent_title = title

我怎么才能得到一个不同的列表(或者如果不可能的话,我可以用一个不同的列表来包装它)在我指定的三个人之上的所有工作?

4

2 回答 2

2

请试试这个。我没有测试过这个。

SELECT  distinct *
FROM    jobs
START   WITH ( city, title ) IN 
     ( SELECT city, title
       FROM   people
       WHERE  name IN ( 'Bill', 'Jim', 'Jane' )
     )
CONNECT BY PRIOR parent_title = title;
于 2010-05-20T06:07:02.290 回答
1

这应该工作:

SQL> SELECT *
  2    FROM jobs
  3   START WITH (title, city) IN (SELECT title, city FROM people)
  4  CONNECT BY PRIOR parent_title = title;

TITLE              CITY           PAY PARENT_TITLE
------------------ ------- ---------- ------------
Associate Marketer Chicago       7500 
Salesman           Houston       5000 CEO
CEO                USA         100000 
于 2010-05-20T08:14:00.520 回答