oracle 如何编写一个PGQL查询,匹配到顶点之间的未知跳数?

js5cn81o  于 2023-08-03  发布在  Oracle
关注(0)|答案(1)|浏览(102)

我有一个关系数据库模式,如:


的数据
DDL

CREATE TABLE Persons
(
 Name VARCHAR2(100) NOT NULL,
 CONSTRAINT Persons_PK PRIMARY KEY (Name)
);
 
CREATE TABLE Groups
(
 Name VARCHAR2(100),
 CONSTRAINT Groups_PK PRIMARY KEY (Name)
);
 
CREATE TABLE Person_Group_Connections
(
 Person_Name VARCHAR2(100),
 Group_Name VARCHAR2(100),
 CONSTRAINT PG_Connection_PK PRIMARY KEY (Person_Name, Group_Name),
 CONSTRAINT PG_Connection_Persons_FK FOREIGN KEY (Person_Name) REFERENCES Persons (Name),
 CONSTRAINT PG_Connection_Groups_FK FOREIGN KEY (Group_Name) REFERENCES Groups (Name)
);

insert into Persons (name) values ('Person A');
insert into Persons (name) values ('Person B');
insert into Persons (name) values ('Person C');
insert into Persons (name) values ('Person D');
insert into Persons (name) values ('Person E');
insert into Persons (name) values ('Person F');

insert into Groups (name) values ('Group 1');
insert into Groups (name) values ('Group 2');
insert into Groups (name) values ('Group 3');
insert into Groups (name) values ('Group 4');

insert into Person_Group_Connections (Person_Name, Group_Name) values ('Person A', 'Group 1');
insert into Person_Group_Connections (Person_Name, Group_Name) values ('Person B', 'Group 1');
insert into Person_Group_Connections (Person_Name, Group_Name) values ('Person B', 'Group 2');
insert into Person_Group_Connections (Person_Name, Group_Name) values ('Person C', 'Group 2');
insert into Person_Group_Connections (Person_Name, Group_Name) values ('Person D', 'Group 2');
insert into Person_Group_Connections (Person_Name, Group_Name) values ('Person D', 'Group 3');
insert into Person_Group_Connections (Person_Name, Group_Name) values ('Person E', 'Group 3');
insert into Person_Group_Connections (Person_Name, Group_Name) values ('Person F', 'Group 3');

字符串
然后我定义一个有顶点和边的Proerty图:

CREATE PROPERTY GRAPH my_graph
  VERTEX TABLES (
    Persons LABEL Person PROPERTIES ( name ),
    Groups LABEL Group PROPERTIES ( name )
  )
  EDGE TABLES (
    Person_Group_Connections as PG_Connection
    SOURCE KEY ( person_name ) REFERENCES Persons ( name )
    DESTINATION KEY (group_name) REFERENCES Groups ( name )
    LABEL person_connected_to_group
  ) OPTIONS (PG_VIEW);



如果不知道跳数,如何编写将两个“Person”顶点链接在一起的查询?
例如:“Person A”如何连接到“Person F”我希望查询返回:"Person A" "Person B" "Person D" "Person F""Person A" → "Group 1" → "Person B" → "Group 2" → "Person D" → "Group 3" → "Person F"
或者是否有更好的方法来定义属性图?

SELECT *
FROM MATCH (P1:Person)-/:person_connected_to_group*/->(g:Group)<-/:person_connected_to_group*/-(P2:Person) on my_graph
WHERE P1.name = 'Person 1' and P2.name = 'Person 2'

6fe3ivhb

6fe3ivhb1#

您能检查一下下面的查询是否符合您的要求吗?
使用PG视图:

SELECT COUNT(e) AS distance
FROM MATCH ANY (p1:Person)-[e]-+(p2:Person) ON my_graph
WHERE p1.name = 'Person A' AND p2.name = 'Person F'

个字符
图形服务器(PGX)还支持以下查询:

SELECT ELEMENT_NUMBER(v1) AS num, v1.name
FROM MATCH ANY (p1:Person)-[g]-+(p2:Person) ON my_graph
     ONE ROW PER STEP (v1, e, v2)
WHERE p1.name = 'Person A' AND p2.name = 'Person F'
ORDER BY num
+----------------+
| num | name     |
+----------------+
| 1   | Person A |
| 3   | Group 1  |
| 5   | Person B |
| 7   | Group 2  |
| 9   | Person D |
| 11  | Group 3  |
+----------------+

的字符串

相关问题