MySQL query to give priority to a LIKE

1

I am writing to you because I would like to make a query in MySQL giving priority to a LIKE. eg: I have a search engine where they put "Old House" ... and I search in the fields subject, document and number the words "old house", "house" and "old" without an ORDER BY

What I want to achieve is to order the query that first shows me the coincidence in "Old house" then those of "home" and then those of "old", can it be done in a query?

Example of the query I do in this case:

SELECT * FROM documentos WHERE (tema LIKE '%Casa vieja%' OR documento LIKE 
'%Casa vieja%' OR numero LIKE '%Casa vieja%' OR tema LIKE '%Casa%' OR 
documento LIKE '%Casa%' OR numero LIKE '%Casa%' OR tema LIKE '%vieja%' OR 
documento LIKE '%vieja%' OR numero LIKE '%vieja%' ) 

Thank you very much

    
asked by Pedro 01.12.2018 в 14:17
source

3 answers

1

what's wrong because not at the end of your query you include the order by (field1, field2, camop3)

    
answered by 01.12.2018 в 14:29
1

Will it serve you as a UNION?

SELECT * 
FROM documentos 
WHERE 
    tema LIKE '%Casa vieja%'
    OR documento LIKE '%Casa vieja%' 
    OR numero LIKE '%Casa vieja%'
UNION
SELECT *
FROM documentos
WHERE
    tema LIKE '%Casa%'
    OR documento LIKE '%Casa%' 
    OR numero LIKE '%Casa%'
UNION
SELECT *
FROM documentos
WHERE
    OR tema LIKE '%vieja%' 
    OR documento LIKE '%vieja%' 
    OR numero LIKE '%vieja%' 

Update 1 .

Viewing this post.

link

I was inspired to do something of this style.

SELECT * 
FROM documentos 
ORDER BY
    tema LIKE '%Casa vieja%' DESC,
    documento LIKE '%Casa vieja%' DESC,
    numero LIKE '%Casa vieja%' DESC,
    tema LIKE '%Casa%' DESC,
    documento LIKE '%Casa%' DESC,
    numero LIKE '%Casa%' DESC,
    tema LIKE '%vieja%' DESC,
    documento LIKE '%vieja%' DESC,
    numero LIKE '%vieja%' DESC
    
answered by 03.12.2018 в 16:56
0

You can use Order By Fields, and specify the order in which you need that information, in the following link you can find more on the subject: Order by field

The following code

SELECT name, species FROM 'pet'
ORDER BY FIELD(species, 'dog','cat','snake','bird'), name ASC

Would have an exit like the following

|name     | species|
|Bowser   |  dog   |
|Buffy    |  dog   |
|Fang     |  dog   |
|Claws    |  cat   |
|Fluffy   |  cat   |
|Slim     |  snake |
|Chirpy   |  bird  |
|Whistler |  bird  |
    
answered by 04.12.2018 в 03:27