Order sql query by first letter

I am trying to order my mysql queries based on the first letter, but every method I used limits my search.

Example MySQL table Cost of computer services Computer services abc dynamic computer services

If I search for computer services , I want the results to be returned as:

 **Name** computer services abc computer services dynamic computer services 

I use the fullsearch mysql text, but if I use the name LIKE 'c%' , I do not do the other two results, for example.

 SELECT name FROM table WHERE match('name') against('computer services*' IN BOOLEAN MODE) AND name LIKE 'c%'; 

This will only result in a refund.

  • computer services

But I want him to come back:

  • computer services
  • computer services abc
  • dynamic computer services

I am new to mysql full text.

+7
source share
1 answer

Use the order by clause, which first matches the β€œstarts with” case. I use not like here because boolean returns 0 or 1, and we want to cancel this to match the first one first.

 SELECT name FROM table WHERE match('name') against('computer services*' IN BOOLEAN MODE) ORDER BY name NOT LIKE 'computer services%', name; 
+2
source

All Articles