【求解】求一条简单的SQL语句!100分答谢~【求解】【求解】【求解】【求解】【求解】【求解】有满意答案立马结贴!

月之点点 2010-01-26 09:42:19
表 A(name:名字,type:第几层 1为大类,2为二级类,3为3级类,typeid:父类的ID 比如Typeid=2 意思就是他的父类的ID=2)
ID name type typeid
1 1 1 0
2 2 2 1
3 3 3 2
4 4 3 2
表 B(Aid:关联表A的id)
ID name Aid
1 1 3
2 2 3
3 3 4

要求:我现在只知道第一层的ID(1)
1、要求取出(大类)下面的(2级类)并且取出表B中包含的个数 显示为:

1级别类(id:1)(数量3)
丨--2级别类(id:2)(数量3)
丨--3级别类(id:3)(数量2)
丨--3级别类(id:4)(数量1)

谢谢了 求SQL语句或者 存储过程。。。有满意答案立马结贴!
...全文
270 30 打赏 收藏 转发到动态 举报
写回复
用AI写文章
30 条回复
切换为时间正序
请发表友善的回复…
发表回复
月之点点 2010-01-26
  • 打赏
  • 举报
回复
谢谢大家了。。已经解决了。。。。
fwacky 2010-01-26
  • 打赏
  • 举报
回复

create table table_A
(ID int, name int , type int, typeid int)
insert into table_A
select 1 , 1 , 1 , 0 union all
select 2 , 2 , 2 , 1 union all
select 3 , 3 , 3 , 2 union all
select 4 , 4 , 3 , 2

create table table_B
(ID int, name int , Aid int)
insert into table_B
select 1 , 1 , 3 union all
select 2 , 2 , 3 union all
select 3 , 3 , 4


select *,cnt =(select count(1) from table_B where aid =table_A.id) into #table3 from table_A where [type] =3;
select *,cnt=(select sum(cnt) from #table3 where typeid =table_A.id) into #table2 from table_A where [type] = 2;
select *,cnt=(select sum(cnt) from #table2 where typeid =table_A.id) into #table1 from table_A where [type] = 1;

select * from #table1
union all
select * from #table2
union all
select * from #table3
drop table #table1,#table2,#table3

ID name type typeid cnt
----------- ----------- ----------- ----------- -----------
1 1 1 0 3
2 2 2 1 3
3 3 3 2 2
4 4 3 2 1
月之点点 2010-01-26
  • 打赏
  • 举报
回复
[Quote=引用 26 楼 mbh0210 的回复:]
试试我上面的Sql语句

建议楼主修改表结构,在增加数据的时候维护好数量.或者增加一个缓存表,来避免类似的查询
[/Quote]
没事。要生成静态页面的。。。。无所谓了
mbh0210 2010-01-26
  • 打赏
  • 举报
回复
试试我上面的Sql语句

建议楼主修改表结构,在增加数据的时候维护好数量.或者增加一个缓存表,来避免类似的查询
蘋果虫 2010-01-26
  • 打赏
  • 举报
回复
DataSet treeDs;
public DataSet creDS()
{
treeDs = newdb.Select("select * from a ");//newdb.select 是查询方法
return treeDs;

}
public void InitTree(TreeNodeCollection Nds, string parentId)
{
TreeNode NewNode;
DataRow[] rows = treeDs.Tables[0].Select("typeid ='" + parentId + "'");
foreach (DataRow row in rows)
{
string aa = row["type"].ToString();
class_name = sum_tree(row["type"].ToString()).ToString();
NewNode = new TreeNode(row["type"].ToString()
Nds.Add(NewNode);
InitTree(NewNode.ChildNodes, row["typeid "].ToString());
}
}
mbh0210 2010-01-26
  • 打赏
  • 举报
回复

declare @t table([ID] int, [name] int , [type] int , [typeid] int )
insert into @t values( 1 , 1 , 1 , 0 )
insert into @t values( 2 , 2 , 2 , 1 )
insert into @t values( 3 , 3 , 3 , 2 )
insert into @t values( 4 , 4 , 3 , 2 )

declare @t1 table([ID] int , [name] int , [Aid] int )
insert into @t1 values( 1 , 1 , 3 )
insert into @t1 values( 2 , 2 , 3 )
insert into @t1 values( 3 , 3 , 4 )


declare @TopLevel int
set @TopLevel = 1



select a.*,
( select count(*) from @t1 where [Aid] in(
select [id] from @t where type = a.Type and [id] = a.[id]
union
select [id] from @t where type = a.Type + 1 and [typeid] = a.[id]
union
select [id] from @t where type = a.Type + 2 and [typeid] in(
select [id] from @t where type = a.Type + 1 and [typeid] = a.[id])
)) as '数量'
from @t a where a.type in( @TopLevel , @TopLevel+1 , @TopLevel+2)
Dream_Hunter_ 2010-01-26
  • 打赏
  • 举报
回复
http://topic.csdn.net/u/20080409/16/1fb7d941-b1a1-4326-a936-230ddf057cbe.html
看有没有帮助
蘋果虫 2010-01-26
  • 打赏
  • 举报
回复
递归表 当然用递归了.................
月之点点 2010-01-26
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 zhouyanfss 的回复:]
select t1.*,t2.*,t3.*
from A t1,A t2,A t3
where t1.id=1 and t2.typeid=a.id and t3.typeid=t2.id

表B
select aid ,count(0) as num
from B
group by aid

表B的结果存到程序的里,当做字典表(该结果应该不是很大,放内存里好了),你可以存在hashtable dictionary等。。。
查看某个ID的次数时,直接用ID做KEY就能返回Value
这样应该是最快的方法了

[/Quote]

where 后面用等于 是取不出来的。。。要是 大类下面有100个2级类。。没个2级2下面有100个3级类呢? 最少也得用in
since_net_lyc 2010-01-26
  • 打赏
  • 举报
回复
up!
月之点点 2010-01-26
  • 打赏
  • 举报
回复
我真晕了。。
就没有高手吗。。。
快来啊。。
1级别类(id:1)(数量3)
丨--2级别类(id:2)(数量3)
丨--3级别类(id:3)(数量2)
丨--3级别类(id:4)(数量1)

应该显示:
----------------结果----------------------------------------
/* ID name type typeid 数量
----------- ----------- ----------- ------------- ----------
1 1 1 0 3
2 2 2 1 3
3 3 3 2 2
4 4 3 2 1

应该这么显示

不会那么简单的。。。。。
zhouyanfss 2010-01-26
  • 打赏
  • 举报
回复
select t1.*,t2.*,t3.*
from A t1,A t2,A t3
where t1.id=1 and t2.typeid=a.id and t3.typeid=t2.id

表B
select aid ,count(0) as num
from B
group by aid

表B的结果存到程序的里,当做字典表(该结果应该不是很大,放内存里好了),你可以存在hashtable dictionary等。。。
查看某个ID的次数时,直接用ID做KEY就能返回Value
这样应该是最快的方法了
段传涛 2010-01-26
  • 打赏
  • 举报
回复
递归绑定树的代码我有 ? 数据库的我没有
xiangqianxi 2010-01-26
  • 打赏
  • 举报
回复
学习了
sxinhe 2010-01-26
  • 打赏
  • 举报
回复
不是在后台可以写代码的,然后绑定到 TREEVIEW 里面可以不?
月之点点 2010-01-26
  • 打赏
  • 举报
回复
1级别类(id:1)(数量3)
丨--2级别类(id:2)(数量3)
丨--3级别类(id:3)(数量2)
丨--3级别类(id:4)(数量1)


主要是扩后里的数量。。。。。


别中午了。就现在吧。。
asdfg_ 2010-01-26
  • 打赏
  • 举报
回复
貌似一条sql语句达不到这种效果,一般是一级一级的绑定时出现吧
先绑定第一层,每次绑定时根据他的id查找父Id为这个id的数据,绑定,在绑定查到的这些数据也是每次都找父ID为当前ID的数据,递归下去 直到没有子节点时返回。
灵雨飘零 2010-01-26
  • 打赏
  • 举报
回复
帮顶!
Mark ,中午休息时间帮你搞定!!
月之点点 2010-01-26
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 njlywy 的回复:]
写个递归算法好了
[/Quote]
我逻辑也知道。但是我SQL是新手。写不出来。。
加载更多回复(10)

62,254

社区成员

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

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

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

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