SQL查询查找所有可能的路径
我有这个表:
create table testtb (c1 number, c2 number);
insert into testtb values (1, 100);
insert into testtb values (2, 100);
insert into testtb values (3, 100);
insert into testtb values (3, 101);
insert into testtb values (4, 101);
insert into testtb values (5, 102);
commit;
我正在努力想出一个 SQL 查询,当 where 子句是这样时,它会返回以下结果:“c2=100”
结果集:
c1 c2
-- ---
1 100
2 100
3 100
3 101
4 101
结果集包含“3,101”的原因是因为它可以通过“3,100”访问”。对于“4,101”也是如此:可通过 -> 到达“3,101”-> “3,100”。
更新:该表包含相似性连接后来自 2 个不同数据集的标识符。因此,我们的想法是允许用户通过任何标识符进行搜索并显示两个数据集之间所有可能的匹配项。这就是为什么当用户搜索“c2=100”时,我还想显示“3,101”和“4,101”以显示完整的匹配图。
谢谢。
I have this table :
create table testtb (c1 number, c2 number);
insert into testtb values (1, 100);
insert into testtb values (2, 100);
insert into testtb values (3, 100);
insert into testtb values (3, 101);
insert into testtb values (4, 101);
insert into testtb values (5, 102);
commit;
I'm struggling to come up with SQL query that would return the following result when where clause is this : "c2=100"
result set:
c1 c2
-- ---
1 100
2 100
3 100
3 101
4 101
The reason result set contains "3,101" is because it's reachable through "3,100". And same for "4,101" : reachable through -> "3,101" -> "3,100".
UPDATE: This table contains identifiers from 2 different data sets after similarity join. So the idea is to allow user to search by any identifier and show all possible matches between two datasets. That is why when user searches for "c2=100" I also want to show "3,101" and "4,101" to show full graph of matches.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
与 jonearles 答案相同的想法,但使用递归子查询分解:
Same idea as jonearles answer, but using recursive subquery factoring:
尝试使用子查询...从您最初的帖子中推断出这一点,希望它有所帮助。
(我是一个 MSSQL 人员,所以如果这没有 100% 映射到 PL-SQL,我深表歉意,但你明白了)
编辑:
抱歉,我看到你也想要 4,101。那么也许有两层子查询?
Try a subquery... inferring this from your initial post, hope it helps.
(I'm a MSSQL person so apologies if this doesn't map 100% to PL-SQL but you get the idea)
Edit:
Sorry, I see you also want 4,101. Maybe two levels of subquery then?