select * from v_coll_port where
collid in (select collid from v_coll_port where pollid=@pollID
group by collid having count(collid) > 1)
select * from V_coll_port where portid in(
select portid from v_coll_port where pollid=@pollID
group by portid having count(portID)>1)
select * from v_coll_port where pollid=@pollID and collid in (
select collid from v_coll_port group by collid having count(collid) = 1
) and portID in (
select portID from v_coll_port group by portID having count(portID) = 1
)
以上代码造出一对多、多对一和一对一的三种关系并列表,在后台得到表格后做如下处理
DataTable dt12N = ds.Tables[0];
DataTable dtn21 = ds.Tables[1];
DataTable dt121 = ds.Tables[2];
GridNotStyle g = new GridNotStyle(2,"black","white","white");
for(int i=0;i<dt12N.Rows.Count;i++)
{
DataRow r = dt12N.Rows[i];
if(i==0)
{
g.SetHead(r["collName"].ToString()+"<br>编号:"+r["collID"].ToString()+"^r^"+dt12N.Rows.Count,
this.getPortName(r["portID"].ToString())+"<br>编号:"+r["portID"].ToString());
}
else
{
g.SetHead(this.getPortName(r["portID"].ToString())+"<br>编号:"+r["portID"].ToString());
}
}
for(int i=0;i<dtn21.Rows.Count;i++)
{
DataRow r = dtn21.Rows[i];
if(i==0)
{
g.SetHead(r["collName"].ToString()+"<br>编号:"+r["collID"].ToString(),
this.getPortName(r["portID"].ToString())+"<br>编号:"+r["portID"].ToString()+"^r^"+dtn21.Rows.Count);
}
else
{
g.SetHead(r["collName"].ToString()+"<br>编号:"+r["collID"].ToString());
}
}
foreach(DataRow r in dt121.Rows)
{
g.SetHead(r["collName"].ToString()+"<br>编号:"+r["collID"].ToString(),
this.getPortName(r["portID"].ToString())+"<br>编号:"+r["portID"].ToString());
}
只是这样的方法只能处理一对多、多对一和一对一都单独存在的情况,倘若是向错的多对多,还真是没有想到更好的办法
本文探讨了使用SQL进行复杂关系查询的方法,并展示了如何通过不同样式处理一对多、多对一及一对一的数据关系,以便更直观地理解数据结构。

736

被折叠的 条评论
为什么被折叠?



