Split search term to enlarge results and sort results according to the term

Asked

Viewed 34 times

0

Let’s say I have a table called carros. In this table I have some registered cars. Ex:

ID | Name    
1  | Corsa Amarelo
2  | Camaro 2 Portas Amarelo
3  | Camaro V12 Amarelo
4  | Camaro Esportivo Amarelo
5  | Fusca Amarelo
6  | Camaro Amarelo
7  | Gol Amarelo
8  | Ferrai Vermelha
9  | Ferrari 490 cv vermelha

If I use the following query SELECT * FROM carros WHERE carros_name LIKE %camaro amarelo% I will result in just the following line

6  | Camaro Amarelo

If I just split the search terms like this carros_name LIKE %camaro amarelo% OR carros_name LIKE %camaro% OR carros_name LIKE %amarelo% the result comes all mixed.

But I would like to get as a result everything that has the words of the search term, organizing the results by the term more similar on. Something like:

ID | Name
1  | Camaro Amarelo
4  | Camaro V12 Amarelo
5  | Camaro Esportivo Amarelo
3  | Camaro 2 Portas Amarelo    
2  | Corsa Amarelo
6  | Fusca Amarelo
7  | Gol Amarelo

As you can see in the result above, the result comes with a classification where the term searched appears first, results with all the words of the search in 2 seconds and finally any result that has any word of the term searched.

  • 1

    Note: 'ranking' does not exist, the right term is classificar or ordenar

  • 1

    Use the match ... against (https://dev.mysql.com/doc/refman/5.5/en/fulltext-search.html) instead of like...

  • @Inkeliz this solved! Thank you very much! If you formulate a reply I can vote as the best.

1 answer

0

Why not use at the end an "ORDER BY carros_name ASC"? So it has returns the result in alphabetical order if and what it wants.

Browser other questions tagged

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