fulltext search not returning total count

孤街浪徒 提交于 2020-01-05 09:04:45

问题


Desperate for help on this, please help!

I am running a fulltext search with mysql. My data is held in two tables, so I have to run separate matches on each table and add the relevancy together as you can see in my MySQL statement.

The problem is that I am trying to get a COUNT for the total posts a question has in my post table. However this is only returned to me when the match is found in the question table, not if the match is found in the posts table? Any idea why?

Please tell me in more information is needed. Thanks,

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

Found the answer.

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:


Make sure that there is a full text index created on each table.

MySQL can't make a fulltext index across multiple tables. Therefore you should use an index on each table, and do a join to retrieve the rows that match your text.



来源:https://stackoverflow.com/questions/9548235/fulltext-search-not-returning-total-count

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!