单向连接和“n度分离”在 MySQL 中?
我有一个具有整数 ID 的实体表,我们将其称为实体。在另一个表中,我通过“从”、“到”列以及它们之间的关系类型(我们将此表称为“关系”)来在这些实体之间建立单向关系。实体可能是“双向”的,具有两个相应的单向关系,并且整体事物是一个图形或网络。
我正在编写一个例程,我可以向它传递一个实体 ID 以及要进行的分离度,然后它返回所传递 ID 的许多关系中的每个实体 ID。我不知道如何编写这个例程。这种迭代的性质超出了我对存储过程的经验。有任何线索如何开始吗?
I have a table of entities with integer IDs, let's call it Entities. In another table, I have one way relationships between these entities by having a column for "From", "To" and what kind of relationship they have (let's call this table Relationships). Entities might be "two way" by having two corresponding one way relationships and the overall thing is a graph or web.
I'm looking to write a routine where I can pass it an Entity ID and how many degrees of separation to go and it returns every entity ID within that many relationships of the passed ID. I have no idea how to write this routine. The iterative nature of this is beyond my experience with stored procedures. Any clues how to start this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于具有 from_id 和 to_id 列的表关系
应该适用于任何图形,查找以级别给出的距离内的所有连接节点。
For a table relationships with columns from_id and to_id
Should work for any graph, finding all connected nodes within distance given as level.
MySQL 中没有
with
语句,因此您需要一个临时表。伪代码:看起来 piotrm 已经为您编写了所需的函数。
There are no
with
statements in MySQL, so you'll need a temporary table. Pseudo-code:Looks like piotrm wrote the needed function for you.