如何从两个用户之间的连接表中检索连接用户的集群?
该表由连接在一起的成对用户组成。以下是一个假设的示例:
user1, user2
a, b
a, c
c, a
c, d
d, e
a, e
j, n
g, n
f, n
通过从表中随机选取一个用户(user1 或 user2),我想检索所选用户所属的整个连接集群。例如,如果选择了用户 d,则查询(或算法)应返回连接
a, b
a, c
c, a
c, d
d, e
a, e
有谁知道如何形成查询语句或创建算法来检索连接集群?
谢谢你!
The table consists of pairs of users which are connected together. The following is a hypothetical example:
user1, user2
a, b
a, c
c, a
c, d
d, e
a, e
j, n
g, n
f, n
By randomly picking up a user from the table (user1 or user2) I would like to retrieve the whole cluster of connections to which the selected user belongs. For example if the user d is selected the query (or an algorithm) should return connections
a, b
a, c
c, a
c, d
d, e
a, e
Does anyone know how to form a query statement or create an algorithm to retrieve the connections cluster?
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通过使用递归 CTE,如下所示:
By using a recursive CTE, like so:
在 SQL 中对树和更一般的图形进行建模很棘手,但可以完成。
你可以用谷歌搜索“部分爆炸SQL”关键字,你会发现很多参考资料。
您可能会在此处找到一种在 MySql 中对非常相似的问题进行建模的方法。
Modeling trees and more general graphs in SQL is tricky, but can be done.
You may google for the "part explosions SQL" keywords, and you'll find a lot of references.
You may find a way to model a very similar problem in MySql here.