简体   繁体   English

获取 SQL Server 图数据库中的所有朋友

[英]Get all friends in SQL Server graph database

I'm trying to retrieve all friends of a particular person using SQL Server graph processing我正在尝试使用 SQL Server 图形处理检索特定人的所有朋友

Person table as node Person表作为node

╔════╦═══════╗
║ Id ║ Name  ║
╠════╬═══════╣
║  1 ║ David ║
║  2 ║ Suraj ║
║  3 ║ John  ║
║  4 ║ Ryan  ║
╚════╩═══════╝

likes as an edge //for simplicity I am not using auto-generated ids here likes作为edge // 为简单起见,我没有在这里使用自动生成的 ID

╔════╦═══════╦═══════╦═══════════════════╗
║ Id ║ From  ║  To   ║      Remarks      ║
╠════╬═══════╬═══════╬═══════════════════╣
║  1 ║ David ║ Suraj ║ David likes Suraj ║
║  2 ║ David ║ John  ║ David likes John  ║
║  3 ║ John  ║ Ryan  ║ John likes Ryan   ║
╚════╩═══════╩═══════╩═══════════════════╝

My graph query to find all friends of John would be like this我查找 John 的所有朋友的图形查询将是这样的

select p1.name, p2.name [friend]
from person p1, likes l, person p2 
where p1.name = 'John' and match(p1-(l)->p2)

and this will return the below result set这将返回以下结果集

╔══════╦════════╗
║ name ║ friend ║
╠══════╬════════╣
║ John ║ Ryan   ║
╚══════╩════════╝

The problem is we got all the people John likes, which excludes the people who likes John(in this case David).问题是我们得到了所有约翰喜欢的人,这排除了喜欢约翰的人(在这种情况下是大卫)。 In real world, if a person is a friend of me, I am also a friend of him right?.在现实世界中,如果一个人是我的朋友,那么我也是他的朋友,对吗? I know I can use union here to find all people who likes John and add up to the above.我知道我可以在这里使用union来找到所有喜欢 John 的人并将其加起来。 But It will make the case worst for scenarios where finding friend of friends.但是对于寻找朋友的朋友的情况,它会使情况变得最糟。 Can we do it more intuitively tweaking with the Match or the arrows我们可以更直观地使用Match或箭头进行调整吗

Expected Result预期结果

+------+--------+
| Name | Friend |
+------+--------+
| John | Ryan   |
| John | David  |
+------+--------+

update : expected result added更新:添加了预期结果

The reason you are getting this answer is due to the directed nature of the edge.您得到此答案的原因是边缘的定向性质。

In real world, if a person is a friend of me, I am also a friend of him right?在现实世界中,如果一个人是我的朋友,那么我也是他的朋友,对吗?

This depends on the domain.这取决于域。 For example, this is true for Facebook or LinkedIn, however it is not true for something like Twitter where just because you follow me does not mean I follow you.例如,这对 Facebook 或 LinkedIn 来说是正确的,但对于像 Twitter 这样的东西来说就不是这样了,因为你关注我并不意味着我关注你。 This is a design decision you need to understand when developing a graph data model.这是您在开发图形数据模型时需要了解的设计决策。

I am not familiar with the syntax for SQL Graph but you can likely traverse the edges bidirectionally by removing the > from the MATCH step like this:我不熟悉 SQL Graph 的语法,但您可以通过从 MATCH 步骤中删除>来双向遍历边缘,如下所示:

select p1.name, p2.name [friend]
from person p1, likes l, person p2 
where p1.name = 'John' and match(p1-(l)-p2)

At least this is how you would do it in another graph pattern matching syntaxes.至少这是您在另一种图形模式匹配语法中的做法。 If this doesn't work then you will likely have to do a UNION .如果这不起作用,那么您可能必须执行UNION

SQL Server graph is directional. SQL Server 图是有方向的。 So you那么你

  • either need to add both edges if you want the friendship to be mutual.如果您希望友谊是相互的,则需要添加两条边。
  • or, because MATCH doesn't allow OR , you would have to use UNION of two queries where each uses the directionality differently:或者,因为MATCH不允许OR ,您必须使用两个查询的 UNION ,其中每个查询使用不同的方向性:
select p1.name, p2.name [friend]
from person p1, likes l, person p2 
where p1.name = 'John' and match(p1-(l)->p2)

UNION

select p2.name, p1.name [friend]
from person p1, likes l, person p2 
where p2.name = 'John' and match(p1-(l)->p2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM