数据结构如下
id name parentid m p
1 中国 0 1 2
2 广东 1 2 3
3 广州 2 1 2
4 天河 3 3 4
5 上社 4 5 7
......
想统计出广东下m的集合
id name parentid m p
1 中国 0 1 2
2 广东 1 2 3
3 广州 2 1 2
4 天河 3 3 4
5 上社 4 5 7
......
想统计出广东下m的集合
解决方案 »
- 一个sql查询问题~~~~~~~~~~~~~~~~~~~~~~~~~
- 请教:如何返回前一页面?注:前一在页有二个值传入.
- 求跳蚤市场 源码 (asp.net)
- asp.net从SQL查询数据很慢如何解决!!急……
- 怎么用.net程序新建一个 WEB 站点 [不是建虚拟目录]------------------求助
- 如何在DATAGRID的HEADER上或FOOTER上加一句话
- asp.net如何删除文件?
- 市场上有net2003卖吗?asp.net开发网站方便吗?
- 为什么我的机器上主autoMation不能创建对象
- (五)最有人气的散分帖(请给出URL)
- 同理,判断文章发布时间是否为 "最近2个星期内"的文章,比如 今天是 16月18号,那么就应该从6月2号(星期一)算起 怎么实现?
- NBearV3的学习步骤???请高手指点一二!
Queue<id> ids = new Queue<id>();
ids.Add(2);
while (ids.Count > 0)
{
List<record> children = FindChildren(ids.Dequeue());
for (int i = 0; i < children.Count; ++i)
{
ms.Add(children[i].m);
ids.Enqueue(children[i].id);
}
}根据排序规则什么的可以做一些优化
没明白意思说清楚:
id name parentid m p
1 中国 0 1 2
2 广东 1 2 3
3 广州 2 1 2
4 天河 3 3 4
5 上社 4 5 7 得到广东下m的集合就是id为3,4,5的m值的和
{
SqlConnection sc = new SqlConnection("连接字符串");
SqlDataAdapter sda = new SqlDataAdapter("select * from 表名", sc);
sc.Open();
DataSet ds = new DataSet();
sda.Fill(ds);
List<int> list = GetmListFromName(ds, "广东");
int count = 0;//count就是所求啦
foreach (int i in list)
count += i;
Console.WriteLine(count);
sc.Close();
} static List<int> GetmListFromName(DataSet ds, string name)
{
if (ds.Tables[0].Rows.Count <= 0)
return null;
List<int> list = new List<int>();
string expression = "name='" + name + "'";
DataRow[] foundRows = ds.Tables[0].Select(expression);
foreach (DataRow dr in foundRows)
{
int id = Convert.ToInt32(dr["id"]);
list.AddRange(GetChildList(ds, id));
}
return list;
} static List<int> GetChildList(DataSet ds, int parentid)
{
List<int> list = new List<int>();
string expression = " parentid='" + parentid + "'";
DataRow[] foundRows = ds.Tables[0].Select(expression);
foreach (DataRow dr in foundRows)
{
int id = Convert.ToInt32(dr["id"]);
int m = Convert.ToInt32(dr["m"]);
list.Add(m);
list.AddRange(GetChildList(ds,id));
}
return list;
}
int sum =0;
int id=1 ;privite void getsum(int id, int value)
{
id=id+1;
if(id>=3)
{
sum = value+sum;
value = //somemethod get value
getsum(id,value);
}
}getsum(1,0);result = sum
1 中国 0 1 2
2 广东 1 2 3
3 广州 2 1 2
4 天河 3 3 4
5 上社 4 5 7
楼上的都是些神仙,看看他这个字段,parentid,乱的,能统计出来???
id name parentid m p
1 中国 0 1 2
2 广东 1 2 3
3 广州 2 1 2
4 天河 2 3 4
5 上社 2 5 7
insert into tb select 1,'中国',0,1,2
insert into tb select 2,'广东',1,2,3
insert into tb select 3,'广州',2,1,2
insert into tb select 4,'天河',3,3,4
insert into tb select 5,'上社',4,5,7drop function f_cidcreate function f_Cid(@ID char(3))
returns @t_level table(ID char(3),Level int)
AS
BEGIN
DECLARE @Level int
SET @Level=1
INSERT @t_Level SELECT @ID,@Level
WHILE @@ROWCOUNT>0
BEGIN
SET @Level=@Level+1
INSERT @t_Level SELECT a.ID,@Level
FROM tb a,@t_Level b
WHERE a.parentid=b.ID
AND b.Level=@Level-1
END
RETURN
ENDSELECT a.*
FROM tb a,f_Cid(2) b
WHERE a.ID=b.IDid name parentid m p
2 广东 1 2 3
3 广州 2 1 2
4 天河 3 3 4
5 上社 4 5 73,2,4,7