在CRM项目中建立联系人之间的家庭关系

时间:2012-05-30 09:34:50

标签: c# sql-server sql-server-2008 tree

我想在联系人之间建立家庭关系,并发现联系人之间隐藏的关系,并使用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的父。 我怎么能这样做?

1 个答案:

答案 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作为父母 - 孩子)?