Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql order by using search string

i have a mysql query like:

select * from employee 
  where name like '%ani%'

I want my results to be order by starting with ani for eg. my results should be comming as: {anil,anirudha,rani, ...} starting with ani first and then followed by other characters.

Can anybody please help me on this.

like image 205
jijo pidiyath Avatar asked Apr 15 '26 00:04

jijo pidiyath


1 Answers

Something like

select * from employee where name like '%ani%' order by locate('ani', name) asc, name asc

Should do the trick ... (sort by position of the substring in the whole string)

like image 70
Ezekiel Rage Avatar answered Apr 17 '26 15:04

Ezekiel Rage



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!