SQL搜索两列以获得最佳结果

时间:2010-05-23 16:33:53

标签: sql

我想执行sql搜索,我希望得到最好的结果。我试了一些东西,但是效果不好。我有两个名为subject和content

的列

例如,我们会在主题和内容区域搜索“搜索此关键字”文字。首先,我搜索“搜索此关键字”,然后搜索“搜索”和“此”和“关键字”

我想在顶部检索主题的结果,我想检索喜欢“搜索此关键字”的最佳结果。我的查询有时效果不好。

我该如何撰写此查询

谢谢..

3 个答案:

答案 0 :(得分:1)

我认为您说要对数据库执行多个SQL查询,然后合并结果并在内容匹配上为主题匹配设置“权重”。

select messageid, textstring, max(weight) from (
-- exact subject match
select messageid,  substr(subject,1,100) textstring, 100 weight
from mytable
    where subject='search this keywords'
union
-- partial subject match
select messageid,  substr(subject,1,100), 90 weight
from mytable
    where subject like '%search this keywords%'
union
select messageid,  substr(subject,1,100), 80 weight
from mytable
    where subject like '%search%'
union
select messageid,  substr(subject,1,100), 80 weight
from mytable
    where subject like '%this%'
union
select messageid,  substr(subject,1,100), 80 weight
from mytable
    where subject like '%keywords%'    
union
-- partial content match
select messageid,  substr(content,1,100), 70 weight
from mytable
    where content like '%search this keywords%'
union
select messageid,  substr(content,1,100), 60 weight
from mytable
    where content like '%search%'
union
select messageid,  substr(content,1,100), 60 weight
from mytable
    where content like '%this%'
union
select messageid, substr(content,1,100), 60 weight
from mytable
    where content like '%keywords%'    
)
group by 
    messageid, textstring,

答案 1 :(得分:0)

试试这个

select * from (
Select sch, rank,
case when sch like '%search this keywords%' then 0
when sch like '%search%' then 1
when sch like '%this%' then 2
when sch like '%keywords%' then 3 end ord
from
(
select subject as sch, 1 as rank from mytable
union all
select content, 2 as rank from mytable
) as x
) as y
where ord is not null
order by rank, ord

答案 2 :(得分:0)

实现像表一样的简单“全文搜索”将是一种方式。

CREATE TABLE YourTable (id int, subject varchar(256), content varchar(8000))

CREATE TABLE Keywords (key_id int, keyw varchar(50), relevanceModifier float)
CREATE TABLE SubjectsKeywords (key_fk int, yourTable_fk int, quantity int)
CREATE TABLE ContentKeywords (key_fk int, yourTable_fk int, quantity int)

在YourTable中插入时,触发一个触发器:
按空格,逗号等将主题和内容列拆分为单词。
可选地,避免像“the”,“他们”,“to”等“停止词语”。如果我没有弄错,这就叫做词干。
应将每个单词插入表SubjectsKeywords,ContentKeywords和Keywords中 (可选)设置relevantModifier。一个非常简单的标准是使用字符串长度 (可选)计算每个事件并跟踪其数量字段。

然后您的查询将是这样的:

select max(t.relevance), yourtable.id, MAX([subject]), MAX(content)
from
(
    /* exact match and 'contains' match */
    select 100 as relevance, id
    from YourTable 
    where [subject] like '%search this keywords%'  
    UNION 
    /* keyword match */
    select 70 as relevance, yt.id
    from YourTable as yt
    join SubjectsKeywords on id = yourTable_fk
    join Keywords as k on k.id = key_fk
    where keyw in ('search', 'this', 'keywords')
    UNION
    select 40 as relevance, id
    from YourTable 
    where [subject] like '%search this keywords%'
    UNION 
    select 10 as relevance, yt.id
    from YourTable as yt
    join ContentKeywords on yt.id = yourTable_fk
    join Keywords as k on k.id = key_fk
    where keyw in ('search', 'this', 'keywords')
) as T
join yourtable on t.id = yourtable.id
group by t.id
order by max(relevance) desc
, yourtable.id ASC /*So that the result will always be in the same order*/

注意:
如果你几乎无法控制你的应用程序,或者这是一个维护噩梦,触发器就是一种方法 稍后你可以通过添加soundex来改进它,这样你就可以搜索拼错的关键词 RelevanceModifier,Quantity字段可用于计算更相关的结果。
因为它可能足够快,它可能对您的应用程序有用,作为自动完成功能,在这种情况下,您希望将结果限制为最多256个。

我希望这会给你和想法,所以你决定什么最适合你。