甲骨文兄弟结构
Oracle Sibling Structure
我有一个在数据库中存储相等记录的结构table。你可以认为这些记录是兄弟姐妹。例如我在这个 table 中有两条记录; 1=2 和 1=3。我需要一个查询,该查询将 return 给定记录的所有兄弟姐妹。让我举个例子;
这是我的 table,有两列:
create table SIBLINGSTEST(col1 number, col2 number);
我有 2 条记录,1=2 和 1=3
insert into SIBLINGSTEST values(1,2);
insert into SIBLINGSTEST values(1,3);
我认为使用 connect by 是针对这种情况的最佳解决方案,并编写了以下查询:
SELECT * FROM SIBLINGSTEST
START WITH (col1 = 1 or col2 = 1)
CONNECT BY NOCYCLE (
(PRIOR col1 = col1) or
(PRIOR col1 = col2) OR
(PRIOR col2 = col1) or
(PRIOR col2 = col2))
此查询 return 的正确结果,return 两行。
如果我使用 2 作为参数,查询也 运行 正确,return 再次查询两行。
但是如果我使用 3 作为参数,查询不会像我预期的那样 运行,只 returning 起始行。
SELECT * FROM SIBLINGSTEST
START WITH (col1 = 3 or col2 = 3)
CONNECT BY NOCYCLE (
(PRIOR col1 = col1) or
(PRIOR col1 = col2) OR
(PRIOR col2 = col1) or
(PRIOR col2 = col2))
我想知道为什么 2 和 3 的结果不同。任何帮助或想法都会得到应用。
谢谢。
我按预期得到了你上次查询的两行:
SQL> SELECT * FROM SIBLINGSTEST
2 START WITH (col1 = 3 or col2 = 3)
3 CONNECT BY NOCYCLE (
4 (PRIOR col1 = col1) or
5 (PRIOR col1 = col2) OR
6 (PRIOR col2 = col1) or
7 (PRIOR col2 = col2));
COL1 COL2
---------- ----------
1 3
1 2
但是我不会选择这样建模。如果您真正想要记录 1、2、3 是兄弟姐妹,那么我会使用:
create table siblings_group (group_id number);
create table people (person_id number, group_id number);
insert into siblings_group values (1);
insert into people values (1, 1);
insert into people values (2, 1);
insert into people values (3, 1);
然后找到 3 的所有兄弟姐妹:
SQL> select person_id from people where group_id =
2 (select group_id from people where person_id=3);
PERSON_ID
----------
1
2
3
我有一个在数据库中存储相等记录的结构table。你可以认为这些记录是兄弟姐妹。例如我在这个 table 中有两条记录; 1=2 和 1=3。我需要一个查询,该查询将 return 给定记录的所有兄弟姐妹。让我举个例子;
这是我的 table,有两列:
create table SIBLINGSTEST(col1 number, col2 number);
我有 2 条记录,1=2 和 1=3
insert into SIBLINGSTEST values(1,2);
insert into SIBLINGSTEST values(1,3);
我认为使用 connect by 是针对这种情况的最佳解决方案,并编写了以下查询:
SELECT * FROM SIBLINGSTEST
START WITH (col1 = 1 or col2 = 1)
CONNECT BY NOCYCLE (
(PRIOR col1 = col1) or
(PRIOR col1 = col2) OR
(PRIOR col2 = col1) or
(PRIOR col2 = col2))
此查询 return 的正确结果,return 两行。
如果我使用 2 作为参数,查询也 运行 正确,return 再次查询两行。
但是如果我使用 3 作为参数,查询不会像我预期的那样 运行,只 returning 起始行。
SELECT * FROM SIBLINGSTEST
START WITH (col1 = 3 or col2 = 3)
CONNECT BY NOCYCLE (
(PRIOR col1 = col1) or
(PRIOR col1 = col2) OR
(PRIOR col2 = col1) or
(PRIOR col2 = col2))
我想知道为什么 2 和 3 的结果不同。任何帮助或想法都会得到应用。
谢谢。
我按预期得到了你上次查询的两行:
SQL> SELECT * FROM SIBLINGSTEST
2 START WITH (col1 = 3 or col2 = 3)
3 CONNECT BY NOCYCLE (
4 (PRIOR col1 = col1) or
5 (PRIOR col1 = col2) OR
6 (PRIOR col2 = col1) or
7 (PRIOR col2 = col2));
COL1 COL2
---------- ----------
1 3
1 2
但是我不会选择这样建模。如果您真正想要记录 1、2、3 是兄弟姐妹,那么我会使用:
create table siblings_group (group_id number);
create table people (person_id number, group_id number);
insert into siblings_group values (1);
insert into people values (1, 1);
insert into people values (2, 1);
insert into people values (3, 1);
然后找到 3 的所有兄弟姐妹:
SQL> select person_id from people where group_id =
2 (select group_id from people where person_id=3);
PERSON_ID
----------
1
2
3