mysql 家谱关系_mysql – 家谱树算法

第一个问题,设计数据模式:我将层次结构保留为父行的外键.这很简单.

第二个问题,检索后代/后代:正如你解释的那样,问题来自于选择:选择一些人和所有后代的后代.要解决此问题,您应该创建一个新的树表.此表包含对:与具有所有祖先(及其自身)的人的组合:

people( id, name, id_parent)

people_tree( id, id_ancestor, distance )

注意,使用此结构很容易查询层次结构.示例:某人的所有后代:

select people.*, distance

from

people p

inner join

people_tree t

on ( p.id = t.id)

where

id_ancesor = **sombody.id **

你可以与远方玩,只有祖父母,孙子女等…

最后一个问题,保持树:树必须始终是数据.您应该自动执行此操作:触发人员或CRUD操作的存储过程,

EDITED

因为这是一个家谱树,每个人必须有两个参考,父母和母亲:

people( id, name, id_parent, id_mother)

然后,需要2棵树:

parent_ancestors_tree( id, id_ancestor, distance )

mother_ancestors_tree( id, id_ancestor, distance )

大卫要求提供样本数据:

people: id name id_parent id_mother

1 Adam NULL NULL

2 Eva NULL NULL

3 Cain 1 2

.. ...

8 Enoc 3 5

parent_ancestors_tree id id_ancestor distance

(Adam) 1 1 0

(Eva) 2 2 0

(Cain) 3 3 0

3 1 1

(Enoc) 8 8 0

8 3 1

8 1 2

mother_ancestors_tree id id_ancestor distance

(Adam) 1 1 0

(Eva) 2 2 0

(Cain) 3 3 0

3 2 1

(Enoc) 8 8 0

-- here ancestors of Enoc's mother --

问候.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值