SQL查询查找所有可能的路径

发布于 2024-10-19 02:13:17 字数 687 浏览 3 评论 0原文

我有这个表:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

把时间冻结 2024-10-26 02:13:17
select distinct c1, c2
from testtb
connect by nocycle prior c1 = c1 or prior c2 = c2
start with c2 = 100
order by c1, c2;
select distinct c1, c2
from testtb
connect by nocycle prior c1 = c1 or prior c2 = c2
start with c2 = 100
order by c1, c2;
只为守护你 2024-10-26 02:13:17

与 jonearles 答案相同的想法,但使用递归子查询分解:

  WITH pathtb(c1,c2) AS
  (
  SELECT c1,c2 FROM testtb WHERE c2=100
  UNION ALL
  SELECT testtb.c1,testtb.c2 FROM
     testtb JOIN pathtb ON (pathtb.c1=testtb.c1 or pathtb.c2=testtb.c2)
  ) CYCLE c1,c2 set cycle TO 1 default 0
  SELECT DISTINCT c1,c2 FROM pathtb WHERE cycle=0
  ORDER BY c1,c2

Same idea as jonearles answer, but using recursive subquery factoring:

  WITH pathtb(c1,c2) AS
  (
  SELECT c1,c2 FROM testtb WHERE c2=100
  UNION ALL
  SELECT testtb.c1,testtb.c2 FROM
     testtb JOIN pathtb ON (pathtb.c1=testtb.c1 or pathtb.c2=testtb.c2)
  ) CYCLE c1,c2 set cycle TO 1 default 0
  SELECT DISTINCT c1,c2 FROM pathtb WHERE cycle=0
  ORDER BY c1,c2
几味少女 2024-10-26 02:13:17

尝试使用子查询...从您最初的帖子中推断出这一点,希望它有所帮助。

select * from testtbl where c1 in (select c1 from testtbl where c2=100)

(我是一个 MSSQL 人员,所以如果这没有 100% 映射到 PL-SQL,我深表歉意,但你明白了)

编辑:
抱歉,我看到你也想要 4,101。那么也许有两层子查询?

    select *
    from testtbl
    where c2 in
    (select c2 from testtbl where c1 in (select c1 from testtbl where c2=100))

Try a subquery... inferring this from your initial post, hope it helps.

select * from testtbl where c1 in (select c1 from testtbl where c2=100)

(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?

    select *
    from testtbl
    where c2 in
    (select c2 from testtbl where c1 in (select c1 from testtbl where c2=100))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文