我有两个表,一个是大类表,一个是大类的分类表
大类表btable字段有 bid,name
数据 1 a
2 aa
3 ccc
4 dd
分类表stable字段有 sid,bid,userid 其中bid是大类表的id字段的外键
数据 1 2 1
2 2 2
3 4 2
4 3 3
5 2 2
6 1 2
7 1 2
8 2 2
====================================================================
我想根据分类表中获得userid=2这样的数据
bid name 次数
2 aa 3
1 a 2
4 dd 1也就是说,通过连表获取某个用户引用大类表的信息及次数。求实现此功能的最优算法。谢谢!!!
大类表btable字段有 bid,name
数据 1 a
2 aa
3 ccc
4 dd
分类表stable字段有 sid,bid,userid 其中bid是大类表的id字段的外键
数据 1 2 1
2 2 2
3 4 2
4 3 3
5 2 2
6 1 2
7 1 2
8 2 2
====================================================================
我想根据分类表中获得userid=2这样的数据
bid name 次数
2 aa 3
1 a 2
4 dd 1也就是说,通过连表获取某个用户引用大类表的信息及次数。求实现此功能的最优算法。谢谢!!!
解决方案 »
- asp.net IIS发布
- 用例编号的编写规则
- 动态页面生成静态页面怎么搞?
- global中解析xml的问题
- gridview中的dropdownlist问题.
- 一直搞不明白,做网页应该用Dreamweaver,还是应该用asp.net?它们两个一起用?若一起联合使用,那么界面与后台之间应该如何合作与设置??
- 一个关于树型结构框架显示的问题
- 关于Response.Write("<script language=javascript>confirm('dkl')</script>")
- 谁在win2003下装过realserver9.0
- 请问如何获取datagrid选定某行某列的值,不是datagrid.item[e.item.itemindex].cell[0]
- 做下载时如何禁止使用下载工具???
- 两个DATALIST嵌套,后找到控件的值怎么取不到值呢?
insert into btable select 1,'a'
insert into btable select 2,'aa'
insert into btable select 3,'ccc'
insert into btable select 4,'dd'create table stable(sid int,bid int,userid int)
insert into stable select 1,2,1
insert into stable select 2,2,2
insert into stable select 3,4,2
insert into stable select 4,3,2insert into stable select 5,2,2
insert into stable select 6,1,2
insert into stable select 7,1,2
insert into stable select 8,2,2--
--我想根据分类表中获得userid=2这样的数据
-- bid name 次数
-- 2 aa 3
-- 1 a 2
-- 4 dd 1 select a.bid,a.name,count(1) as '次数'
from btable a join stable b on a.bid=b.bid
where b.userid=2
group by a.bid,a.namebid name 次数
1 a 2
2 aa 3
3 ccc 1
4 dd 1
insert @btable select 1, 'a'
union all select 2, 'aa'
union all select 3, 'ccc'
union all select 4, 'dd' declare @stable table (sid int,bid int,userid int)
insert @stable select 1 , 2 , 1
union all select 2 , 2, 2
union all select 3 , 4 , 2
union all select 4 , 3 , 3
union all select 5 , 2 , 2
union all select 6 , 1 , 2
union all select 7 , 1 , 2
union all select 8 , 2 , 2 select max(a.bid) as bid,name,count(a.bid) as '次数'
from @btable a left join @stable b on a.bid=b.bid
where b.userid=2 group by (name)
/*
bid name 次数
----------- ----- -----------
1 a 2
2 aa 3
4 dd 1*/
insert @btable select 1, 'a'
union all select 2, 'aa'
union all select 3, 'ccc'
union all select 4, 'dd' declare @stable table (sid int,bid int,userid int)
insert @stable select 1 , 2 , 1
union all select 2 , 2, 2
union all select 3 , 4 , 2
union all select 4 , 3 , 3
union all select 5 , 2 , 2
union all select 6 , 1 , 2
union all select 7 , 1 , 2
union all select 8 , 2 , 2 select a.bid,a.name,count(1) as '次数'
from @btable a join @stable b on a.bid=b.bid
where b.userid=2 group by a.bid,a.name
order by 次数 descbid name 次数
2 aa 3
1 a 2
4 dd 1用楼上的数据,我的数据好象弄错了