How to sort by the relevance of Like?

Asked

Viewed 799 times

8

I’m making an autocomplete that makes a request to a url, which in turn queries in the database.

This consultation is made with the LIKE. I use %$termo% to be able to consult, but I would like to sort the result according to the relevance of the LIKE, and not in alphabetical order.

How can I do that?

Example: When searching car, the order being something like that:

SELECT * FROM tabela WHERE campo LIKE '%car%' ORDER BY aqui_vem_a_magica();
  • Carrthe
  • carthe
  • reforcar
  • lapcar

That is the order not by alphabetical order, but by the proximity of characters that are at the beginning of the word...

  • Because the carro has to come first than caro?

  • Because I want to... Kkkkk... I’m joking... Because the idea is to get first the like which is more for the beginning of the word and the rest later

  • I made a serious typo, sorry

  • I can post a solution in MSSQL and you adapted to Mysql? :)

  • @Marconi would be cool if I could adapt the question to a global SQL context

  • Jeferson posted in Mysql and I in Sql-Server, the context became an almost totally "global". = D

Show 1 more comment

3 answers

7


You can use the function LOCATE of MySQL within the ORDER BY:

SET @termo := 'car';

SELECT *
  FROM tabela
 WHERE campo LIKE CONCAT('%', @termo, '%')
 ORDER BY LOCATE(@termo, campo);

Explaining the query above:

  • The variable @termo is created and receives the text that will be searched in the table;
  • The CONCAT will put % at the beginning and end of the text, resulting in %car% in the case of the example, which indicates that car may be in any position of the campo;
  • The clause ORDER BY will take into account the position where the searched text is in the column campo, returning first those that have the text car at the beginning of the text;

The result will not be the same as in the example because the method shown above does not take into account only the beginning and the end of the column campo. Take into account the position in the whole sentence by presenting it as follows:

╔═══╦════════════╗
║   ║ campo      ║
╠═══╬════════════╣
║ 1 ║ caro       ║
║ 2 ║ carro      ║
║ 3 ║ colocar    ║
║ 4 ║ reforcar   ║
╚═══╩════════════╝

LOCATE

... Returns the position of the first Occurrence of substring replace in string str.

In free translation:

... returns the position of the first occurrence of a substring replace in a string str.


CONCAT

For quoted strings, concatenation can be performed by placing the strings next to each other.

In free translation:

For highlighted strings, concatention can be performed by placing the strings one after the other.

  • 1

    Although the others are more voted, I chose to mark this as useful because I have more explanations. Novice users will benefit more from explanations such as these than simply a ready-made solution (understand that this is not a criticism of other answers, just a differential).

6

You can do this by checking which position this string is in your field, with the function INSTR, for example:

SELECT INSTR('meucarrovermelho','car'); 

In this case it will return the number 3, since "car" is in the 3 position of the word "meucarrovermelho".

With this you can do your ordering initially by it and then alphabetically.

Your query would look like this:

SELECT * 
FROM tabela 
WHERE campo LIKE '%car%' 
ORDER BY INSTR(campo ,'car'), campo;
  • 2

    It has how to put in the answer what the function INSTR ago?

  • sure, I’ll complement

  • Ah, cool, the INSTR returns the position the string is in :p

4

How about using Case When in the Order By?

 select * from MinhaTabela
 WHERE texto LIKE '%car%'
 order by
 case when texto like 'car%' then 0 else 1 end

Sort text in ascending order where to start with car.

Observing: This solution works on Sql-Server, because I don’t have a server Mysql installed. Case of Mysql works a little different, so you need to adjust.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.