开发者

SQL Match Against or Some other method to search two tables

开发者 https://www.devze.com 2023-03-26 04:47 出处:网络
I\'ve look at the questions on here but can\'t find an answer that specifically matches what i\'m trying to achieve...

I've look at the questions on here but can't find an answer that specifically matches what i'm trying to achieve...

Basically I have an SQL table that stores FAQ information. The table in question are below with the primary keys in bold.

Table: questions, Fields: id_question, question, answer, status, etc......

Table: question_tags, Fields: id_question, tag

Table: tags, Fields: tags

The question_tags table is essentially a link between the question table and the tag table (i.e. many to many).

What I am looking to do is implement something like SQL MATCH AGAINST so when a user enters a question into the search box (PHP) the system looks up the questions table and searches the questions for the k开发者_JAVA百科eywords in the search term. I can get this to work but what I want to be able to do is include the tags within the search. The reason for this is that I believe there are a number of different ways the same question can be using different words. The tags are supposed to compliment the question when searching. I understand that this is impossible using the MATCH AGAINST function within SQL so i'm kind of stuck.

Is there a way around this limitation?

Or instead of having each tag in a separate column of a separate table (linked with a third table) as above, should I simply create another field in the questions table for the entire tag string?

Or does anyone else have any other suggestions?

The solution will not be working with large datasets to begin with but it would be helpful if the solution was scalable.

Thanks


Tags may be a good candidate for de-normalization for this use case.

One possibility would be something like

SELECT * FROM 
(
 SELECT Question, Answer, Status, MATCH(title,category) AGAINST ('keyword') as rank
 FROM questions 
 WHERE MATCH(Question,Answer) AGAINST ('Keyword')
 UNION 
 SELECT Question, Answer, Status, MATCH(Tag) AGAINST ('keyword')
 FROM questions Q 
 INNER JOIN question_tags QT   
     ON Q.id_question = QT.id_question
 INNER JOIN tags T
     ON QT.id_tag = T.id_tag 
 WHERE MATCH(Tag) AGAINST ('keyword')
) R
ORDER BY rank

Basically doing a FT search against question/answer and another on tags and unioning the resultset and then sorting by rank.

0

精彩评论

暂无评论...
验证码 换一张
取 消