2012-03-03 73 views
1

絕望的幫助,請幫助!全文搜索不返回總數

我正在用mysql進行全文搜索。我的數據保存在兩個表中,因此我必須在每個表上運行單獨的匹配項,並將相關性添加到一起,如您在我的MySQL語句中所見。

問題是,我正在嘗試獲取問題在我的發佈表中發佈的帖子總數。然而,只有在問題表中找到匹配項時纔會返回給我,而不是在帖子表中找到匹配項的情況下呢?任何想法爲什麼?

請告訴我更多的信息是必要的。謝謝,

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 

回答

0

確保每個表上都創建了全文索引。

MySQL無法跨多個表創建全文索引。因此,您應該在每個表上使用索引,並執行聯接以檢索與您的文本匹配的行。

+0

我已經這樣做了,這就是爲什麼查詢返回一行,只是字段total_answers爲0.感謝您的幫助。 – yehuda 2012-03-03 18:09:32