[英]SQL query for many-to-many self-join
I have a database table that has a companion many-to-many self-join table alongside it. 我有一个数据库表,旁边有一个伴随的多对多自联接表。 The primary table is
part
and the other table is alternate_part
(basically, alternate parts are identical to their main part with different #s). 主表是
part
,另一个表是alternate_part
(基本上,替代部分与主要部分相同,但具有不同的#)。 Every record in the alternate_part
table is also in the part
table. 在每一条记录
alternate_part
表也是在part
表。 To illustrate: 为了显示:
`part`
| part_id | part_number | description |
|---------|-------------|-------------|
| 1 | 00001 | wheel |
| 2 | 00002 | tire |
| 3 | 00003 | window |
| 4 | 00004 | seat |
| 5 | 00005 | wheel |
| 6 | 00006 | tire |
| 7 | 00007 | window |
| 8 | 00008 | seat |
| 9 | 00009 | wheel |
| 10 | 00010 | tire |
| 11 | 00011 | window |
| 12 | 00012 | seat |
`alternate_part`
| main_part_id | alt_part_id |
|--------------|-------------|
| 1 | 5 | // Wheel
| 5 | 1 | // |
| 5 | 9 | // |
| 9 | 5 | // |
| 2 | 6 | // Tire
| 6 | 2 | // |
| ... | ... | // |
I am trying to produce a simple SQL query that will give me a list of all alternates for a main part. 我正在尝试生成一个简单的SQL查询,该查询将为我提供主要部分的所有替代项的列表。 The tricky part is: some alternates are only listed as alternates of alternates , it is not guaranteed that every viable alternate for a part is listed as a direct alternate.
棘手的部分是: 有些替代品仅作为替代品列出 ,不能保证零件的每个可行替代品都列为直接替代品。 eg, if 'Part 3' is an alternate of 'Part 2' which is an alternate of 'Part 1', then Part 3 is an alternate of Part 1 (even if the
alternate_part
table doesn't list a direct link). 例如,如果“第3部分”是“第2部分”的替代,而“第2部分”是“第1部分”的替代,则第3部分是第1部分的替代(即使
alternate_part
表未列出直接链接)。 The reverse is also true (Part 1 is an alternate of Part 3). 反之亦然(第1部分是第3部分的替代)。
Basically, right now I'm pulling alternates and iterating through them 基本上,现在我要拉出替代品并遍历它们
SELECT p.*, ap.*
FROM part p
INNER JOIN alternate_part ap ON p.part_id = ap.main_part_id
And then going back and doing the same again on those alternates. 然后返回并再次对这些替代项执行相同操作。 But, I think there's got to be a better way.
但是,我认为必须有一个更好的方法。
The SQL query I'm looking for will basically give me: 我正在寻找的SQL查询基本上会给我:
| part_id | alt_part_id |
|---------|-------------|
| 1 | 5 |
| 1 | 9 |
For part_id = 1, even when 1 & 9 are not explicitly linked in the alternates table. 对于part_id = 1,即使在备用表中未明确链接1和9时也是如此。
Note: I have no control whatever over the structure of the DB, it is a distributed software solution. 注意:我无法控制数据库的结构,它是一种分布式软件解决方案。 Note 2: It is an Oracle platform, if that affects syntax.
注意2:如果这会影响语法,则它是Oracle平台。
You have to create hierarchical tree , probably you have to use connect by prior , nocycle query something like this 您必须创建分层树,可能必须使用before by connect,nocycle查询类似这样的内容
select distinct p.part_id,p.part_number,p.description,c.main_part_id
from part p
left join (
select main_part_id,connect_by_root(main_part_id) real_part_id
from alternate_part
connect by NOCYCLE prior main_part_id = alternate_part_id
) c
on p.part_id = c.real_part_id and p.part_id != c.main_part_id
order by p.part_id
You can read full documentation about Hierarchical queries at http://docs.oracle.com/cd/B28359_01/server.111/b28286/queries003.htm 您可以在http://docs.oracle.com/cd/B28359_01/server.111/b28286/queries003.htm中阅读有关分层查询的完整文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.