如何在分层查询中获取不同的元素列表?
我有一个数据库表,其中包含通过姓名、工作和城市标识的人员。我有第二个表,其中包含每个城市公司中每项工作的层次结构表示。
假设人员表中有 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
我的查询的一部分,以便它从我指定的城市中的三个工作中的每一个开始。
我可以执行三个单独的查询来获取我想要的内容,但这不能很好地扩展。例如:
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
我还能如何获得高于我指定的三个人的所有工作的不同列表(或者如果不可能的话,我可以用一个不同的列表来包装它)?
I have a database table, with people identified by a name, a job and a city. I have a second table that contains a hierarchical representation of every job in the company in every city.
Suppose I have 3 people in the people table:
[name(PK),title,city]
Jim, Salesman, Houston
Jane, Associate Marketer, Chicago
Bill, Cashier, New York
And I have thousands of job type/location combinations in the job table, a sample of which follow. You can see the hierarchical relationship since parent_title is a foreign key to title:
[title,city,pay,parent_title]
Salesman, Houston, $50000, CEO
Cashier, Houston, $25000
CEO, USA, $1000000
Associate Marketer, Chicago, $75000
Senior Marketer, Chicago, $125000
.....
The problem I'm having is that my Person table is a composite key, so I don't know how to structure the start with
part of my query so that it starts with each of the three jobs in the cities I specified.
I can execute three separate queries to get what I want, but this doesn't scale well. 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
How else can I get a distinct list (or I could wrap it with a distinct if not possible) of all the jobs which are above the three people I specified?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请尝试这个。我没有测试过这个。
Please try this. I haven't tested this.
这应该有效:
this should work: