I have a search form on my website that allows users to search for employees in a company. I’m using the input to search a table in a mySql DB. My SELECT state is here:
SELECT title, uid FROM table_data
WHERE title LIKE '%$search_name%' AND blog_id = 6
ORDER BY title ASC LIMIT 50
Currently, a search for ‘Jon’ would result in some like:
Angela Jones
Dejonas Lucero
Ernesto Jon
Jon White
Rick Jonston
Is there a way to have it sort in this order?
search_name%
%search_name
%search_name%
Jon White
Ernesto Jon
Angela Jones
Dejonas Lucero
Rick Jonston
This is one way to do it, although I feel it’s not very elegant:
LIMIT 50
It assigns each title a ‘0’ if it matches ‘search_name%’, 1 if it matches ‘%search_name’, and 2 otherwise (which is ‘%search_name%’ by virtue of the WHERE clause).
Then it sorts by it (ascending).
You could also do it with a
CASE WHEN..THEN..ENDstatement: