0

我有多个 sql 查询:

SELECT SQL_CALC_FOUND_ROWS id, autor, dle_post.date AS newsdate, dle_post.date AS DATE, short_story AS story, dle_post.xfields AS xfields, title, descr, keywords, category, alt_name, comm_num AS comm_in_news, allow_comm, rating, news_read, flag, editdate, editor, reason, view_edit, tags,  '' AS output_comms
FROM dle_post
WHERE dle_post.approve =1
AND MATCH (
title, short_story, full_story, dle_post.xfields
)
AGAINST (
'test'
)
LIMIT 0 , 30

我怎样才能确定title列的优先级,所以结果title将是第一位的?

4

2 回答 2

1

match您可以在中重复order by

SELECT SQL_CALC_FOUND_ROWS id, autor, dle_post.date AS newsdate, dle_post.date AS DATE, short_story AS story, dle_post.xfields AS xfields, title, descr, keywords, category, alt_name, comm_num AS comm_in_news, allow_comm, rating, news_read, flag, editdate, editor, reason, view_edit, tags,  '' AS output_comms
FROM dle_post
WHERE dle_post.approve =1
AND MATCH (
title, short_story, full_story, dle_post.xfields
)
AGAINST (
'test'
)
order by MATCH (title) AGAINST ('test') desc
LIMIT 0 , 30

顺便说一句,你不应该在没有limit子句的情况下使用order by。你可能想要的是更像这样的东西:

SELECT SQL_CALC_FOUND_ROWS id, autor, dle_post.date AS newsdate, dle_post.date AS DATE,
       short_story AS story, dle_post.xfields AS xfields, title, descr, keywords,
       category, alt_name, comm_num AS comm_in_news, allow_comm, rating, news_read, flag,
       editdate, editor, reason, view_edit, tags,  '' AS output_comms,
       match(title, short_story, full_story, dle_post.xfields
            ) against ('test') as Relevancy
FROM dle_post
WHERE dle_post.approve =1
having Relevancy > 0
order by MATCH (title) AGAINST ('test') desc, Relevancy desc
LIMIT 0 , 30
于 2013-06-16T20:02:23.650 回答
0

我找到了这个例子

SELECT *, ( (1.3 * (MATCH(title) AGAINST ('+term +term2' IN BOOLEAN MODE))) + (0.6 *(MATCH(text) AGAINST ('+term +term2' IN BOOLEAN MODE))) ) AS relevance
FROM [table_name]
WHERE ( MATCH(title,text) AGAINST ('+term +term2' IN BOOLEAN MODE) )
HAVING relevance > 0
ORDER BY relevance DESC;

您基本上为列添加权重并按这些权重的总和进行排序。MySQL 有几个很好的搜索引擎可以执行此功能,我推荐Apache Lucene

于 2013-06-16T20:04:22.640 回答