• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

SQL query to select a variety of names

Fuhrmann

Well-known member
#1
I have a variable which contains this name: Roberto Teixeira Alves Freitas

And in my Database I have this names:
Code:
John Carpenter
Roberto Alves
Julian Magnata
Roberto Teixeira
Roberto Freitas
Teixeira Alves
Joey Ramone
Teixeira Roberto

I need to do a SQL query which selects all this variety of the name. The result have to be this:

Code:
Roberto Alves
Roberto Teixeira
Freitas Roberto
Teixeira Alves
Teixeira Roberto
Alves Freitas
There is a way to do that?

I already tried this:

Code:
SELECT Name FROM My_Table WHERE (Name LIKE '%Roberto%" OR Name LIKE '%Teixeira%") AND (Name LIKE '%Alves%" OR Name LIKE '%Freitas%")
But this query will only give me this results:

Code:
Roberto Alves
Teixeira Alves
Teieira Roberto
Freitas Roberto
But I can get this name Alves Freitas and Roberto Teixeira. Any help?
 

Brogan

XenForo moderator
Staff member
#4
So won't you just have to add all variables to each OR array?

Either that or have 2 fields; first_name, last_name.
 

Fuhrmann

Well-known member
#5
So won't you just have to add all variables to each OR array?

Either that or have 2 fields; first_name, last_name.

Thank you Brogan!

This is what I have done, and it works:

Name: Jose Vitor Teixeira de Freitas

Code:
SELECT Name FROM My_Table WHERE  OR (Name like '*jose*' AND Name like '*vitor*')  OR (Name like '*jose*' AND Name like '*teixeira*')  OR (Name like '*jose*' AND Name like '*freitas*')  OR (Name like '*vitor*' AND Name like '*teixeira*')  OR (Name like '*vitor*' AND Name like '*freitas*')  OR (Name like '*teixeira*' AND Name like '*freitas*')  ORDER BY Name ASC
And it works.