Searching for separate words in the same string in Mysql

Asked

Viewed 616 times

0

My question is relatively simple, but I couldn’t find an answer that would really help me:

My user searches for a name in a form: Fulano Silva

The record in my bank is like So-and-so of Tal Silva

SELECT * FROM `dados` WHERE `nome` LIKE '%".Fulano Silva."%'

When performing the search with PHP in Mysql by Fulano Silva using LIKE he will not be able to find the record!

If he had sought only by John Doe or by only by Silva, he would, but because there are more strings between Fulano and Silva (de Tal) the record is not found.

How can I resolve this situation? Should I separate the strings in the sentence and search them individually? How could I do that? Thank you!

1 answer

-1


Place % between all words to be filtered:

SELECT * FROM `dados` WHERE `nome` LIKE '%".Fulano%Silva."%'

I put an example on Sqlfiddle for you to see working

  • ... which can be done through str_replace(): str_replace(' ', '%', $texto);

  • Downvoter, can comment?

  • Why the negative vote?

  • I don’t know either, I’m waiting for an explanation :/

  • @Arturtrapp worked perfectly, that’s what I needed.

Browser other questions tagged

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