库:SQL2000或SQL2005我的实际应用情况如下——
用户表字段:id,name,city,xingqu(即兴趣),sex等字段【记录数10000条】
在用户主页页面,当网民浏览某个用户主页时,进行相似用户的推荐。
推荐相似会员固定为10个:
首先是高相似度推荐【即select top 10 * from [users] where city=贵阳 and xingqu=打篮球 and sex=男】
此时,统计查出来的条数是否达到10条,不足10条(假如为3),则保留所查,再放宽条件查询。
进行第二轮查询【即select top 10-3 * from [users] where city=贵阳 and xingqu=打篮球】
到此,再次统计第二轮查询结果,如果第二轮已达7条,则不再查询,若未到7条(假如4条),则再次放宽查询。
进行第三轮查询【即select top (10-3-4) * from [users] where city=贵阳】像这样的应用,如果不用存储过程,直接在前端代码写SQL语句,效率是差得没法说的。
用存储过程的话,我又不会写,哎,关键是从上面的SQL语句看,第二轮查询将包含第一轮的结果集,第三轮查询又将包含第一二轮的结果集,又该怎么去除重复的会员ID呢?求大神们帮一把,帮写个存储过程,千言万语一句话哈【感谢再感谢】。
相似度查询存储过程
用户表字段:id,name,city,xingqu(即兴趣),sex等字段【记录数10000条】
在用户主页页面,当网民浏览某个用户主页时,进行相似用户的推荐。
推荐相似会员固定为10个:
首先是高相似度推荐【即select top 10 * from [users] where city=贵阳 and xingqu=打篮球 and sex=男】
此时,统计查出来的条数是否达到10条,不足10条(假如为3),则保留所查,再放宽条件查询。
进行第二轮查询【即select top 10-3 * from [users] where city=贵阳 and xingqu=打篮球】
到此,再次统计第二轮查询结果,如果第二轮已达7条,则不再查询,若未到7条(假如4条),则再次放宽查询。
进行第三轮查询【即select top (10-3-4) * from [users] where city=贵阳】像这样的应用,如果不用存储过程,直接在前端代码写SQL语句,效率是差得没法说的。
用存储过程的话,我又不会写,哎,关键是从上面的SQL语句看,第二轮查询将包含第一轮的结果集,第三轮查询又将包含第一二轮的结果集,又该怎么去除重复的会员ID呢?求大神们帮一把,帮写个存储过程,千言万语一句话哈【感谢再感谢】。
相似度查询存储过程
第一轮不用排除
第二轮加条件排除:city=贵阳 and xingqu=打篮球 and sex<>男
第三轮加条件排除:city=贵阳 and xingqu<>打篮球 and sex<>男现在就只有一个问题了,即多次系统查询,怎么合并输出查询结果了,哈哈,笨人还是有开窍的时候哈,嘿嘿
等大神出来哈。
如果用union all select 重复的也查询出来select top 10 * from [users] where city='贵阳' and xingqu='打篮球' and sex='男'
union select top 7 * from [users] where city='贵阳' and xingqu='打篮球' and sex<>'男'
union select top 3 * from [users] where city='贵阳' and xingqu<>'打篮球' and sex<>'男'
(@id int) -- 当前浏览某个用户的id
as
begin
set nocount on
select id,name,city,xingqu,sex into #r from [users] where 1=2 insert into #r(id,name,city,xingqu,sex)
select top 10 a.id,a.name,a.city,a.xingqu,a.sex
from [users] a
inner join (select * from [users] where id=@id) b on
a.city=b.city and a.xingqu=b.xingqu and a.sex=b.sex and a.id<>b.id if (select count(1) from #r)<10
begin
insert into #r(id,name,city,xingqu,sex)
select top (10-(select count(1) from #r)) a.id,a.name,a.city,a.xingqu,a.sex
from [users] a
inner join (select * from [users] where id=@id) b on
a.city=b.city and a.xingqu=b.xingqu and a.id<>b.id
where not exists(select 1 from #r c where c.id=a.id)
end
if (select count(1) from #r)<10
begin
insert into #r(id,name,city,xingqu,sex)
select top (10-(select count(1) from #r)) a.id,a.name,a.city,a.xingqu,a.sex
from [users] a
inner join (select * from [users] where id=@id) b on
a.city=b.city and a.id<>b.id
where not exists(select 1 from #r c where c.id=a.id)
end
select top 10 * from #r
end