I have autocomplete on my site where a user enters a class at his school. It then provides a drop-down list of classes in which there are words that 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 everyone is looking for according to the conditions that the user enters. Suppose the use introduces the words "international econ". He will then search for these two members 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 request. I am doing REPLACE () and CONCAT () so that users can enter depts / courses / sections as a unit, for example. ECON-101 (department and course together). In addition, I inserted a very simplified query that does not take into account how I am currently sorting and grouping.
However, my question is about how I can sort the results. I want the records corresponding to Class_Code to be returned at the top, then Course_Code, then Dept, then Course_Title, finally prof. In other words, I want the results to be sorted by the number of specifications that the user provides in autocomplete. If they know Class_Code, I would like it to be at the top.
I canβt think of a way to make this look that doesnβt require a huge request. So far, the only way I can do this is to overlay a heap of LIKE comparison for each term in each field, and then ORDERing with these aliases. This is equivalent to 5 times the number of words that the user enters into autocomplete. In other words, a huge ugly request.
Is there a simple and effective way to make this look? If there was something like the INSTR () function, which occupied multiple lines for searching, I could just simply compare all the words in the autocomplete with each field and always sort by 5 aliases created by this.
Any input is welcome, even if it is a general sentence that goes beyond the scope of the question. However, I want to note in advance that I cannot use something like FULLTEXT or a search engine such as Sphinx, because I do not feel that it is suitable for a non-intensive fast query like this. In addition, I cannot restructure my database. Therefore, I would like to do this with a good MySQL query for what I already have.
Thanks in advance.