我想在联系人之间建立家庭关系,发现联系人之间的隐藏关系,并在sql server中使用递归显示它。例如:我有一个包含以下内容的表:
User_id_1 User_id_2 relation
1 2 Parent-child
2 3 Brother-brother
3 4 Brother-brother我的存储过程应该发现特定用户id的所有关系。如果用user_id=4调用这个sp,它应该告诉我user_id=4的所有关系,并且它应该知道4是3和2的兄弟,1是4的父母。我怎么能做到这一点?
发布于 2012-05-30 17:45:10
您可以从另外两个表开始,一个用于配置关系(类似于当前的relation列),另一个用于配置递归查找期间要遍历的关系。
Table: Relationship
Id: int
Description: string
Table: RelationshipToRelationship
FromRelationshipId: int
ToRelationshipId: int数据将如下所示:
Relationship
Id Description
== ===============
1 Brother-Brother
2 Parent-ChildRelationshipToRelationship
FromRelationshipId ToRelationshipId
================== ================
1 1
1 2如果我解决了这个问题,我会用一个可行的查询进行更新
我预见到的问题:
最简单的例子(我兄弟的父母就是我的父母),但在其他情况下(我兄弟的孩子不是我的标签关系的问题(对于我来说,你可以将我的兄弟的父母标记为Brother-Brother-Parent-Child).
Parent-Child所有的事情很快就会变得混乱。在您希望关联的每一方之间保持直接关系不是更好吗(即,您的表中的另外2条记录将用户4&2链接为兄弟,4&1链接为父母-孩子)?
https://stackoverflow.com/questions/10813626
复制相似问题