I need to create an SQL query that may or may not have multiple arguments. Example:
Pesquisa 1: "João Goiânia"
Pesquisa 2: "Goiânia João"
Pesquisa 3: "João Advogado Goiânia"
The query should go through three fields: name, city, profession. Since the arguments can be in one, two or all of them.
Imagining a populated table:
id, nome, cidade, profissão
1, Maria, Brasília, Advogado
2, José, Anápolis, Engenheiro
3, João Carlos, Goiânia, Advogado Trabalhista
4, João da Silva, Goiânia, Médico
The result for search 1 and 2 would be records 3 and 4, and for search 3 would be just record 3.
I thought about using campo IN (arg1, arg2, arg3)
, but there is a possibility that the argument is not complete, as in the example. What is the best solution?
My database is: PostgreSQL 9.4