sql多个关键字搜索,单个关键字出现的次数排序
有没有懂的啊?帮帮忙给给解决的方法先谢谢了
在线等答:2596062639
sql2000 的写啊?
改成适合2000的了,用了临时表:
create table key_word(word nvarchar(30))
insert into key_word
select 'JAVA' union all
select 'PHP' union all
select 'asp' union all
select 'aspx' union all
select 'FLEX'
go
drop table tb
go
create table tb(id int,content nvarchar(max))
insert into tb
select 5 , 'ASP,PHP用来做网页都不错ASP,做网页都不错ASP,做网页都不错ASP,做网页都不错ASP,aspx是动态网页'
union all select 6, 'ASP,PHP用来做网页都不错ASP,做网页都不错ASP,做网页都不错ASP,做网页都不错ASP,做网页都不错ASP,做网页都不错ASP,做网页都不错ASP,aspx是动态网页,aspx是动态网页PHP、PHP'
union all select 7, 'ASP,PHP用来做网页都不错ASP,做网页都不错ASP,aspx是动态网页'
go
If OBJECT_ID('tempdb..#t') is not null
drop table #t
If OBJECT_ID('tempdb..#tt') is not null
drop table #tt
select id,
content,
kw.word,
dbo.f_searchSTR(tb.content,kw.word) 次数
into #t
from tb
inner join key_word kw
on 1=1
where kw.word in ('asp','aspx','php')
group by id,content,kw.word
select t.id,content,word,次数,min_count
into #tt
from #t t
inner join
(
select id,min(次数) min_count
from #t
group by id
)tt
on t.ID = tt.ID
select top 1 *
from #tt
where min_count > 0 --次数最少的都大于0,说明所有关键字的次数都大于0
and word = 'asp'
order by abs(次数 - 6 )
/*
idcontent
5ASP,PHP用来做网页都不错ASP,做网页都不错ASP,做网页都不错ASP,做网页都不错ASP,aspx是动态网页
*/