How to search two words in the same Mysql field

Asked

Viewed 1,719 times

5

Good morning.

I have a field on my table called tags, would like to do a search, where two or three words can be considered.

Example: In the field tags, I have some words, like "free", "cabins", "audiometer"... with the query below, my result returns empty, even with the word being inside the field.

Like I’m doing:

SELECT * 
FROM Produtos 
WHERE Tags = 'Audiômetro' OR 'Impedanciômetro' OR 'OEA' OR 'BERA' OR 'Vectonistagmógrafo';

My result only appears if I have ONLY one item in the "tags" field. If I put a phrase, for example, I get no results.

How to proceed?

2 answers

6

First of all, the ideal would be if the tag text were separated from the field, it would be a more scalable solution.

If you really want to use textual search, the solution goes through something like this:

SELECT .... WHERE
     tags LIKE '%tag1%' AND
     tags LIKE '%tag2%' 

and repeats AND as many tags as possible.

There’s another problem to solve:

If the search is by the tag "Audio", you will find the field "Audiometer", which is undesirable.

The solution would be to use the field separator in the LIKE:

 SELECT ... WHERE
      CONCAT( ',' , tags , ',' ) LIKE '%,tag1,%' ...

This whole complexity serves to force the delimitation by commas, and the CONCAT to ensure that you have one at the beginning and one at the end (for the first and last tags).


FIND_IN_SET

If the tag field can be separated by a comma, Mysql has a dedicated function:

SELECT ... WHERE
    FIND_IN_SET( 'tag1', tags ) AND
    FIND_IN_SET( 'tag2', tags ) AND
    FIND_IN_SET( 'tag3', tags ) 
    ...

In any adopted solution, you should always normalize the storage of tags, to remove any spaces, hit accents etc.

  • I had already seen this solution, but since I intend to release 10, 15 tags, I thought it would be less work to put them in one field.

  • still returning empty. I did the test below with the REGEXP and it worked fine. Thanks for your time! You’re the guy!

  • It can be some silly detail of syntax, however if there is some error in the answer it is fundamental that I find and correct so as not to leave anything teaching wrong. I’d have to see your code to see if it was my mistake or yours, but I couldn’t do that here in the answer. Qq thing, if you want, leave a link to the Pastebin in the comments that I spy later.

  • It may even be my mistake here, don’t beat yourself up. Sure, I must have implanted it wrong. Again, thank you very much!

  • 1

    Anyway, I would recommend thinking about a relationship of tables, instead of storing it in text, because when you have many tags, it starts to complicate. Search for substring is slow, either with regex, like or find_in_set

  • I understand. This is possible to happen. I will study this possibility.

  • Still in the matter of normalize can be interesting to store in the database using Pipes "|" as separator and replace in the view so you can always search with SELECT ... WHERE Tags LIKE `%term|% ...

Show 2 more comments

4


You must use the clause IN:

SELECT * 
FROM Produtos 
WHERE Tags IN ('Audiômetro', 'Impedanciômetro', 'OEA', 'BERA', 'Vectonistagmógrafo');

edited

whereas the countryside Tags contains more than one tag the query would look a little different, see if the REGEXP makes things easier:

SELECT * 
FROM Produtos 
WHERE Tags REGEXP 'Audiômetro|Impedanciômetro|OEA|BERA|Vectonistagmógrafo'; 
  • Still returning empty. In my field tags, I’ve separated the words by comma, but no results yet

  • If I just leave a word in the field, it brings me the expected result. But I need more words in the same field.

  • your field Tags contains several tags? type like "OAS,BERA,Vectonistagmógraf"??

  • Exact. I need him to list the products, from the search by tags.

  • tranks :p sometimes think that too

  • 1

    Yes, guys! Thank you for your dedication. Thank you @rLinhares. It was perfect!

Show 1 more comment

Browser other questions tagged

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