Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3610066
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:42:21+00:00 2026-05-18T21:42:21+00:00

I have an autocomplete on my site where a user enters a class at

  • 0

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..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T21:42:22+00:00Added an answer on May 18, 2026 at 9:42 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple autocomplete field that spits out a bunch of suggested words
I have a search box on my site that uses autocomplete to give suggestions,
I have a functioning autocomplete box on my site that I want to work
I have autocomplete text box, that is JQuery based. when i record the macro,
I have an autocomplete field where user can enter lastname firstname middlename in the
I have an autocomplete event that fires correctly once a value is selected. I
I have a autocomplete textbox, in that textbox am getting employee names. If I
I'm building an intranet site for work with an autocomplete feature. I have it
I have a site with a form with autocomplete using jquery. It works fine,
I have CGridView on my site. A wont add autocomplete filter. But when i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.