Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting results on initial terms within a word first

I have an autocomplete dropdown that is generated based on user's input.

Now I want to order the results in such a way that results that start with the term should come first, and then the rest in alphabetical order.

Right now they're just sorted alphabetically in the SQL query.

enter image description here

So I want Vanco... results to come before the rest.

As you can see, the query uses LIKE %term% wild card select at the moment.

like image 268
silkfire Avatar asked Dec 03 '25 23:12

silkfire


1 Answers

Try something along these lines:

ORDER BY 
  CASE WHEN COL LIKE 'term%' THEN 1 ELSE 2 END ASC,
  COL ASC
like image 80
Derek Kromm Avatar answered Dec 05 '25 13:12

Derek Kromm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!