How to do SQL query using like comparator and multiple values out of order

0

I looked for answers here in the community but found nothing specific. I have a page where I can search the names of clients previously registered in a MySQL database. I am currently using the following statements:

PHP: $valor = str_replace(' ', '%', $pesquisa);

SQL: "select * from user where nome like '%".$valor."%' order by nome asc;"

Being PHP variable $ search , comes from a search form on the same page. (The above code allows me to do searches with more than one value). For example, if there is a client in the database named Luiz Henrique da Silva , and I search for Luiz , Luiz Henrique , or even Luiz Silva , this code will work perfectly, listing that user.

However, if I search the name out of order, such as Silva Henrique , or da Silva Luiz , the user will not be listed. How could you enable this reverse search in a simple way?

    
asked by anonymous 01.06.2018 / 07:20

1 answer

1
  

Syntax of 'Like' in SQL:

SELECT (campos) FROM (tabelas) WHERE campo1 LIKE "%variavel%";

  

In the context where you want to use it, it would look more or less like this:

$comando = "SELECT * FROM user WHERE nome LIKE '%$valor%' ORDER BY nome ASC;"

  

But in order to find the Luiz Henrique da Silva search for Luiz Silva , it would not give, only if you had 2 different variables and you could do something like this in your SQL query:

SELECT (campos) FROM (tabelas) WHERE campo1 LIKE "%Luiz%" AND campo1 LIKE "%Silva%";
  

And so it would appear the registration Luiz Henrique da Silva .


I hope I have helped in some way.

    
01.06.2018 / 12:27