在列上选择优先级

时间:2013-06-16 19:53:37

标签: mysql sql select

我有一个多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的结果将是第一个?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

我发现了example

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