求sql算法啊

txgaozhao 2008-06-04 05:44:55
我有两个表,一个是大类表,一个是大类的分类表
大类表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

也就是说,通过连表获取某个用户引用大类表的信息及次数。求实现此功能的最优算法。谢谢!!!
...全文
134 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
txgaozhao 2008-06-04
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 wzy_love_sly 的回复:]
数据库会吃得消吗????????
没问题的 :) 假如这语句sql也吃不消,sql早就被丢弃了
[/Quote]
那我就放心啦,o(∩_∩)o...哈哈
txgaozhao 2008-06-04
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 wzy_love_sly 的回复:]
可以说,这是最好的写法
听人说 left join 比 join 也许好一点点点,但没测试过
[/Quote]
怎么用啊?两者的区别是什么啊???
qq249529304 2008-06-04
  • 打赏
  • 举报
回复
都是高手...
wzy_love_sly 2008-06-04
  • 打赏
  • 举报
回复
数据库会吃得消吗????????

没问题的 :) 假如这语句sql也吃不消,sql早就被丢弃了
johnet520 2008-06-04
  • 打赏
  • 举报
回复
mark
wzy_love_sly 2008-06-04
  • 打赏
  • 举报
回复
可以说,这是最好的写法

听人说 left join 比 join 也许好一点点点,但没测试过
txgaozhao 2008-06-04
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 wzy_love_sly 的回复:]
SQL codedeclare@btabletable(bidint,namenvarchar(5))insert@btableselect1,'a'unionallselect2,'aa'unionallselect3,'ccc'unionallselect4,'dd'declare@stabletable(sidint,bidint,useridint)insert@stableselect1,2,1unionallselect2,2,2unionallselect3,4,2unionallselect4,3,3unionallselect5,2,2unionallselect6,1,2unionallselect7,1,2unionallselect8,2,2selecta.bid,a.name,count(1)as'次数'from@btableajoin@stablebon…
[/Quote]

假如说用户要频繁用到这个查询,数据库会吃得消吗????????
貌似这种连表查询都是很好性能的哦?
wzy_love_sly 2008-06-04
  • 打赏
  • 举报
回复
declare @btable table (bid int,name nvarchar(5))
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 次数 desc


bid name 次数
2 aa 3
1 a 2
4 dd 1



用楼上的数据,我的数据好象弄错了
elvis_gao 2008-06-04
  • 打赏
  • 举报
回复
declare @btable table (bid int,name nvarchar(5))
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

*/

wzy_love_sly 2008-06-04
  • 打赏
  • 举报
回复
create table btable(bid int,name varchar(20))
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,2

insert 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.name


bid name 次数
1 a 2
2 aa 3
3 ccc 1
4 dd 1
wolf_410 2008-06-04
  • 打赏
  • 举报
回复
遍历查询结果集,再各个匹配项计数累加
wzy_love_sly 2008-06-04
  • 打赏
  • 举报
回复
select bid,name,count(1) as '次数' from btable a join stable b on a.bid=b.bid where b.userid=2
zcl26 2008-06-04
  • 打赏
  • 举报
回复
不会,帮顶下

62,074

社区成员

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

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

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

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