sql 血缘关系

hailiu123 2010-03-18 09:06:06
有一张表
id 父id 母id
A A1 A2
B B1 B2
A1 A3 A4
A3 A5 A6
..........

给你一个或多个id ,查询后的得到的结果如下

个体号 父亲 母亲 父亲的父亲 父亲的母亲 母亲的父亲 母亲的母亲 父亲父亲的父亲 父亲父亲的母亲 父亲母亲的父
亲 父亲母亲的母亲 母亲父亲的父亲 母亲父亲的母亲 母亲母亲的父亲 母亲母亲的母亲

所要的就是一个个体的上三代的编号





...全文
356 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
hailiu123 2010-03-18
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 moudy 的回复:]
很简单。
如表名为 aaa

select t1.id as 个体号,t1.父id as 父亲,t1.母id as 母亲,t2.父id as 父亲的父亲,t2.母id as 父亲的母亲,t3.父id as 母亲的父亲,t3.母id as 母亲的母亲,t4.父id as 父亲父亲的父亲,t4.母id as父亲父亲的母亲,t5.父id as 父亲母亲的父亲,t5.母id as 父亲母亲的母,t6……
[/Quote]条件有问题
乐乐010 2010-03-18
  • 打赏
  • 举报
回复
顶,学习。。
mail_ylei 2010-03-18
  • 打赏
  • 举报
回复

protected void Page_Load(object sender, EventArgs e)
{

if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("ID");
dt.Columns.Add("FID");
dt.Columns.Add("MID");

string[] strIDs = new string[] { "1", "2", "张4" };

for (int i = 0; i < strIDs.Length; i++)
{
Select(dt, strIDs[i], 0);
}


Repeater1.DataSource = dt;
Repeater1.DataBind();
}

}

private void Select(DataTable dt, string id, int c)
{
if (id == null || id == "")
{
return;
}

DataSet ds = SqlHelper.ExecuteDataset(CONNECTION_STRING, CommandType.Text, "select ID,FID,MID from tba where ID='" + id + "'");

//判断是否存在
if (ds.Tables[0].Rows.Count == 1)
{
string strID = ConvertToString(ds.Tables[0].Rows[0]["ID"]);
string strFID = ConvertToString(ds.Tables[0].Rows[0]["FID"]);
string strMID = ConvertToString(ds.Tables[0].Rows[0]["MID"]);
DataRow row = dt.NewRow();
row["ID"] = strID;
row["FID"] = strFID;
row["MID"] = strMID;
dt.Rows.Add(row);
if (strFID != "" && strMID != "")
{
c++;
if (c < 4)
{
Select(dt, strFID, c);
Select(dt, strMID, c);
}
c = 0;
}
else if (strFID != "")
{
c++;
if (c < 4)
{
Select(dt, strFID, c);
}
c = 0;
}
else if (strMID != "")
{
c++;
if (c < 4)
{
Select(dt, strMID, c);
}
c = 0;
}
else
{
return;
}

}

}

private string ConvertToString(object obj)
{
if (obj == DBNull.Value)
return "";
else
return ConvertToString(obj.ToString());

}

private string ConvertToString(string obj)
{
return obj.ToString();

}

hailiu123 2010-03-18
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 moudy 的回复:]
很简单。
如表名为 aaa

select t1.id as 个体号,t1.父id as 父亲,t1.母id as 母亲,t2.父id as 父亲的父亲,t2.母id as 父亲的母亲,t3.父id as 母亲的父亲,t3.母id as 母亲的母亲,t4.父id as 父亲父亲的父亲,t4.母id as父亲父亲的母亲,t5.父id as 父亲母亲的父亲,t5.母id as 父亲母亲的母,t6……
[/Quote]
谢谢啊
porschev 2010-03-18
  • 打赏
  • 举报
回复
题目太难看。。。。闪之。。。。。。
moudy 2010-03-18
  • 打赏
  • 举报
回复
很简单。
如表名为 aaa

select t1.id as 个体号,t1.父id as 父亲,t1.母id as 母亲,t2.父id as 父亲的父亲,t2.母id as 父亲的母亲,t3.父id as 母亲的父亲,t3.母id as 母亲的母亲,t4.父id as 父亲父亲的父亲,t4.母id as父亲父亲的母亲,t5.父id as 父亲母亲的父亲,t5.母id as 父亲母亲的母,t6.父id as 母亲父亲的父亲,t6.母id as 母亲父亲的母亲,t7.父id as 母亲母亲的父亲,t7.母id as 母亲母亲的母亲
from aaa t1,aaa t2,aaa t3,aaa t4,aaa t5,aaa t6,aaa t7
where t1.父id = t2.id and t1.母id=t3.id
and t2.父id = t4.id and t2.母id=t5.id
and t3.父id = t6.id and t3.母id=t7.id
and t1.id = 个体号
guoheng90 2010-03-18
  • 打赏
  • 举报
回复
顶,关注中!
丰云 2010-03-18
  • 打赏
  • 举报
回复
写一个存储过程,用临时表+递归实现吧
xxoo2007 2010-03-18
  • 打赏
  • 举报
回复
MARK
gongsun 2010-03-18
  • 打赏
  • 举报
回复
可以分开2张表 存储。

索引id 子id 父id


----------------------

索引id 子id 母id
凤凰涅檠 2010-03-18
  • 打赏
  • 举报
回复
没见过LZ这么NB的

62,047

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧