我想在联系人之间建立家庭关系,并发现联系人之间隐藏的关系,并使用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的父。 我怎么能这样做?
答案 0 :(得分:1)
您可以从另外两个表开始,1用于配置关系(类似于当前的relation
列),另一个用于配置在递归查找期间要走的关系。
Table: Relationship
Id: int
Description: string
Table: RelationshipToRelationship
FromRelationshipId: int
ToRelationshipId: int
数据看起来像这样:
<强>关系强>
Id Description
== ===============
1 Brother-Brother
2 Parent-Child
<强> RelationshipToRelationship 强>
FromRelationshipId ToRelationshipId
================== ================
1 1
1 2
如果/当我解决时,将使用可行的查询进行更新
我预见的问题:
Parent-Child
给我,但您兄弟的孩子需要标记为Brother-Brother-Parent-Child
)。所有人都很快就感到困惑。你是不是更好地维持你想要联系的每一方之间的直接关系(即你的表格中另外2条记录将用户4和2连接为Brother-Brother,4&amp; 1作为父母 - 孩子)?