我有一个非常常见的问题,我正在尝试使用图形查询(sql server 2017)来解决。
- 我想构建一个查询并查找节点中的任何人如何连接到
C
. - 我想构建一个查询并查找节点中的任何人是如何连接的
C
(通过 1 个或 2 个连接)。
这是创建此图的完整脚本:
DROP TABLE IF EXISTS Person;
CREATE TABLE Person (userName VARCHAR(100) PRIMARY KEY) AS NODE;
INSERT INTO Person (userName) VALUES ('A'),('B'),('C'),('D'),('E'),('F');
DROP TABLE IF EXISTS Follow;
CREATE TABLE Follow AS EDGE;
INSERT INTO Follow ($from_id, $to_id) VALUES (
(SELECT $node_id FROM dbo.Person WHERE userName = 'A'),
(SELECT $node_id FROM dbo.Person WHERE userName = 'E')),
((SELECT $node_id FROM dbo.Person WHERE userName = 'E'),
(SELECT $node_id FROM dbo.Person WHERE userName = 'C')),
((SELECT $node_id FROM dbo.Person WHERE userName = 'C'),
(SELECT $node_id FROM dbo.Person WHERE userName = 'A')),
((SELECT $node_id FROM dbo.Person WHERE userName = 'A'),
(SELECT $node_id FROM dbo.Person WHERE userName = 'F')),
((SELECT $node_id FROM dbo.Person WHERE userName = 'F'),
(SELECT $node_id FROM dbo.Person WHERE userName = 'B')),
((SELECT $node_id FROM dbo.Person WHERE userName = 'B'),
(SELECT $node_id FROM dbo.Person WHERE userName = 'F')),
((SELECT $node_id FROM dbo.Person WHERE userName = 'B'),
(SELECT $node_id FROM dbo.Person WHERE userName = 'E')),
((SELECT $node_id FROM dbo.Person WHERE userName = 'E'),
(SELECT $node_id FROM dbo.Person WHERE userName = 'B'));
这个查询不起作用,因为它只给我直接关系:
SELECT Person1.userName as userName1, Person2.userName as userName2
FROM Person as Person1, Follow, Person as Person2
WHERE MATCH(Person1-(Follow)->Person2)
AND Person2.userName = 'C'