I have an autocomplete on my site where a user enters a class at their school. It then provides a dropdown of Classes that have the words they entered somewhere in their fields. The Classes table has the fields Class_ID, Term_ID, Department_Code, Course_Code, Class_Code, Course_Title, Instructor which are all searched by the terms the user puts in. Suppose a use enters the words “international econ”. It would then search these two terms in the fields as follows:
SELECT Class_ID, Department_Code, Course_Code, Class_Code, Course_Title, Instructor FROM Classes AND ((REPLACE(REPLACE(REPLACE(CONCAT(Department_Code, Course_Code, Class_Code), '-', ''), '(', ''), ')', '') LIKE '%international%' OR Instructor LIKE '%international%' OR Course_Title LIKE '%international%') AND (REPLACE(REPLACE(REPLACE(CONCAT(Department_Code, Course_Code, Class_Code), '-', ''), '(', ''), ')', '') LIKE '%econ%' OR Instructor LIKE '%econ%' OR Course_Title LIKE '%econ%')) LIMIT 10
Two quick notes on this query– I do the REPLACE() and CONCAT() so that users can enter depts/courses/sections as a unit, e.g. ECON-101 (A dept and a course together). Also, I’ve pasted a greatly simplified query which leaves out the way I currently sort and group.
However my question deals with how I can sort the results. I want entries matching the Class_Code to be returned at the the top, followed by Course_Code, followed by Dept, then followed by Course_Title, lastly by prof. In other words, I want the results sorted by the amount of specfificity the user provides in the autocomplete. If they know the Class_Code, I’d therefore like it to be at the top.
I can’t think of a way of doing this sort which doesn’t require a huge query. So far the only way I can think of doing it is aliasing a bunch of LIKE comparison for each term to each field, and then ORDERing by those aliases. That’s equivelant to 5 times the number of words the user enters in the autocomplete.. In other words, a huge ugly query.
Is there a simple, efficient way to do this sort? If there was something like an INSTR() function which took multiple strings to search for, I could easily just compare all the words in the autocomplete to each field, and always sort on the 5 aliases produced by that.
Any input welcome, even if it’s a general suggestion that goes beyond the scope of the question. However, in advance I want to note that I’m not open to using something like FULLTEXT or a search system like Sphinx here because I don’t feel its fit for a non-intensive, fast query like this one. Also, I’m not open to restructuring my database. So I’d like to do this with a good MySQL query on what I already have.
Thanks in advance..
Have you considered using the UNION operator? You could separate the different queries into their own SELECT statement, and then join them all together by using UNION. This will automatically keep them in the groups that you want them – that being the order in which you run the SELECT statements.
It may make your query bigger but should be effective.