I’m trying to add a full text search to a system. The query I want to write needs to involve multiple lookups followed by the search (if that’s even possible).
I’ve got a table of teachers and a table of subjects.
teacherProfile
teacherId [int] - primary key
subjectOneId [int]
subjectTwoId [int]
subjectThreeId [int]
teacherBiography [text]
subjects
subjectId [int]
subjectName [text]
So ultimately I want a resultset along the lines of..
teacherId [int]
teacherBiography [text]
( subjectOneName [text] )
( subjectTwoName [text] )
( subjectThreeName [text] )
So these last three fields in brackets are non existant but I do want to perform a text search upon them, do I need to setup a foriegn key constraint (which I’d rather not to do in case of further impacts on the existing system) or is there something more eloquent I can do?
MySQLcannot index views so what you want is impossible inMySQL.You can use an external fulltext indexing engine like Sphinx and load data there using a query with
JOINSAlternatively, you can create a denormalized table:
and fill it with this query:
on a timely basis.
Actually, if all your tables are
MyISAM, you can apply fulltext search queries (in boolean mode) to a join, without having to create a fulltext index.Say, if you are searching for
'+Jones +math +physics', whereJonesis a teacher’s surname andmathandphysicsare subjects, you can do this query:The
MATCH(teacherBiography) AGAINST ('+Jones')will use aFULLTEXTindex onteacher, if any; the secondMATCHwill fine-filter the results.Queries involving the
ORconditions or relevance sorting are more complex, however.