全文搜索不返回总计数

时间:2012-03-03 17:35:31

标签: php mysql full-text-search

急需帮助!

我正在使用mysql运行全文搜索。我的数据保存在两个表中,因此我必须在每个表上运行单独的匹配,并将相关性添加到我的MySQL语句中。

问题在于我试图在帖子表格中找到问题所包含的总帖子的COUNT。但是,只有在问题表中找到匹配时,才会返回给我,而不是在posts表中找到匹配项?知道为什么吗?

请告诉我更多信息需要。谢谢,

SELECT questions. * , 
     posts.post, 
     COUNT(posts.post) -1 AS total_answers, 
     posts.votes, 
     posts.id AS post_id, 
     posts.created, 
     users.id AS user_id, 
     users.username, 
     users.rep, 
     MATCH (questions.title) AGAINST ( '{$keywords}') AS title_relevance,
     MATCH (posts.post) AGAINST ( '{$keywords}') AS post_relevance
FROM questions
     LEFT JOIN posts ON questions.id = posts.question_id
     LEFT JOIN users ON questions.user_id = users.id
WHERE MATCH (questions.title) AGAINST ( '{$keywords}')
    OR MATCH (posts.post) AGAINST ( '{$keywords}')
GROUP BY questions.id
ORDER BY (title_relevance + post_relevance) DESC

找到答案。

SELECT questions. * , posts.post, posts.question_id AS QID, (

SELECT COUNT( posts.post ) 
FROM posts
WHERE question_id = QID
) AS total_answers, posts.votes, posts.id AS post_id, posts.created, users.id AS     user_id, users.username, users.rep, 
MATCH (
questions.title
)
AGAINST (
'humans'
) AS title_relevance, 
MATCH (
posts.post
)
AGAINST (
'humans'
) AS post_relevance
FROM questions
LEFT JOIN posts ON questions.id = posts.question_id
LEFT JOIN users ON questions.user_id = users.id
WHERE MATCH (
questions.title
)
AGAINST (
'humans'
)
OR MATCH (
posts.post
)
AGAINST (
'humans'
)
GROUP BY questions.id
ORDER BY (
title_relevance + post_relevance
) DESC 
LIMIT 0 , 30

1 个答案:

答案 0 :(得分:0)

确保在每个表上创建了全文索引。

MySQL无法跨多个表创建全文索引。因此,您应该在每个表上使用索引,并执行连接以检索与您的文本匹配的行。