How to search MYSQL search for word in title and then in text and sort by title first

Asked

Viewed 238 times

4

I have a table with title and content and wanted to do a search to see if the term is in any of these fields.

If you have, bring the results of the title first and then the content

tabela
id|titulo|conteudo
1 |abc   |oai poa
2 |aaa   |abc
3 |abc   |xcvv
4 |ttt   |rt d

in the above example, in a search for "abc" would like to bring first the ids 1 and 2 and lastly the 3, which does not have abc in the title, but has in the content. And as the second order criterion, put in alphabetical order, I can do only using 1 criterion:

SELECT titulo
FROM tabela
WHERE titulo LIKE '%abc%'
ORDER BY titulo ASC

2 answers

5


I would make it look like what Arthur did, just by assigning the importance of the result to a value. For example:

  • If there is result in the title assigns the result 2
  • If there is result in content assigns to result 1
  • Sum it all, and put it in descending order ( FROM THE HIGHEST RELEVANCE TO A MINOR)

Select would look like this:

SELECT
   titulo, id
FROM
   tabela
WHERE 
    titulo   LIKE '%abc%' 
 OR conteudo LIKE '%abc%' 

ORDER BY (
    CASE WHEN `titulo` LIKE '%abc%'
    THEN 2 # título é o mais importante então o valor é 2
    ELSE 0
    END
  ) + (
    CASE WHEN `conteudo` LIKE '%abc%'
    THEN 1 # conteúdo é o segundo mais importante então o valor é 1
    ELSE 0
    END
  ) DESC;

After the comment "Ah, one more thing, there are items who have the term in the title and in the content, in this case, prioritize title"

Master if you can do something like this:

SELECT
   titulo, id
FROM
   tabela
WHERE 
    titulo   LIKE '%abc%' 
 OR conteudo LIKE '%abc%' 

ORDER BY (
    CASE WHEN `titulo` LIKE '%abc%' AND `conteudo` NOT LIKE '%abc%' 
    THEN 2 # título é o mais importante então o valor é 2
    ELSE 0
    END
  ) + (
    CASE WHEN `conteudo` LIKE '%abc%' AND `titulo` NOT LIKE '%abc%' 
    THEN 1 # conteúdo é o segundo mais importante então o valor é 1
    ELSE 0
    END
  ) + (
    CASE WHEN `conteudo` LIKE '%abc%' AND `titulo` LIKE '%abc%' 
    THEN 2 # valor é 2 pois a relevância é o título
    ELSE 0
    END
  ) DESC

4

  • he brought all, but brought reversed, first came those who have in the content and last that have in the title. Ah, one more thing, there are items who have the term in the title and content, in this case, prioritize title

  • In this case I believe it is only put ASC in the boolean of the order (already corrected in the answer). As it is already prioritizes title, because it will evaluate (title LIKE '%abc%') as true even if the content is also.

  • 1

    worked out, just change asc by desc

  • So Arthur, this select is not working out, see > http://www.sqlfiddle.com/#! 9/35e0c8/1

  • Who is first is just the 'aaa' and should be the 'abc' of life...

  • This is how it works > http://www.sqlfiddle.com/#! 9/8e1ccd/3

Show 1 more comment

Browser other questions tagged

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