sql 语句 请教

来源:百度知道 编辑:UC知道 时间:2024/05/04 15:35:14
问题:
表member是用户注册表;
结构是:
id,user,password
1,liu, 343434
2,wang,4545

表keyword是用户发表的主题表;
id,userid,title
1,1,今天的天气
2,1,喜欢太阳
3,2,出门旅游

表view是用户发表主题被其他用户订阅的信息表
id,titleid,othid
1,2,222
2,2,423
3,1,49
4,2,23
现在想要统计用户所发表的主题累计订阅量总和的排名,请问SQL的语句怎么实现
表keyword中的userid是发表主题的用户注册id;表view中的titleid是发表的某条主题id、othid是订阅该主题的用户的注册id
举例:有两个注册的用户张三、王五,注册的id分别是19和35;张三发表了2条主题(主题id分别是100、101),王五发表了2条主题(主题id分别是200、201);主题100在view表中有3条记录、主题101在view表中有2条记录、主题200在view表中有9条记录、主题201在view表中有1条记录
现在要求按照用户发表的所有主题得到的总的订阅量进行排序

我想keyword表中id字段应该是view表中对应的titleid吧?否则没法关联。

select id,[user],sum(cs)
from
(
select a.userid,count(*) as cs
from keyword a,[view] b
where a.id=b.titleid
)a ,member b
where a.id=b.userid
group by id,[user]
order by 3 desc

select keyword.[title] ,count(keyword.[title]) as 定阅次数 into #tempTable1 from [view],keyword where keyword.userid=[view].titleid group by keyword.[title] order by 定阅次数 desc
select 排序= identity(int,1,1),* into #temptable2 from #temptable1
select * from #temptable2
drop table #temptable1
drop table #temptable2

你所有ID 是指同一个么, 你可以在主题表里 用 COUNT函数 来统计你要统计的用户 返回值是总数