我有一个数据库表,人们通过姓名,工作和城市来识别。我有第二个表格,其中包含每个城市公司中每项工作的分层表示。
假设我在人员表中有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
部分,以便它从三个作业中的每一个开始我指定的城市。
我可以执行三个单独的查询来获得我想要的内容,但这不能很好地扩展。 e.g:
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
除了我指定的三个人之外的所有工作,我怎样才能获得一份不同的清单(或者我可以用一个独特的,如果不可能的包装)?
答案 0 :(得分: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;
答案 1 :(得分: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