Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

190
Visualizações
Postgresql Select anything / everything

I am creating a search tool for Postgres. It will have several text entries (one for each table column). When the text entry is blank, I want the query to match each and every table entry. Is this possible? I have tried:

SELECT name FROM contacts WHERE surname = '*';
SELECT name FROM contacts WHERE surname = *;

As you can imagine, this returns nothing since there is no surname of *, and the second query is invalid. Any ideas?

EDIT:

Because of the nature of what I am doing, a SELECT name FROM contacts is not sufficient. I guess I could make it work, but it would be ugly. I want a WHERE for every column in the table, but if the search for any given column is an empty string, I want it to fetch every entry in the table.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

OK, so I found my answer here. The correct code is:

SELECT name FROM contacts WHERE surname ~* ''; 

returns all entries

SELECT name FROM contacts WHERE surname ~* 'John Doe'; 

returns entries that have a surname of John Doe.

over 4 years ago · Santiago Trujillo Relatório

0

I want the query to match each and every table entry

Just use SELECT without WHERE surname clause. Simple as that.

SELECT name FROM contacts

But if you want to use one query, you can do it this way:

cur.execute(
    """SELECT name FROM contacts 
    WHERE (CASE WHEN %(entry)s != '' THEN surname = %(entry)s ELSE true END)"""
    , {'entry': "John Doe"}
)

As for your answer, you used regular expressions matching. In the question you never mentioned it. So I assume it's not what you wanted. Regular expressions have specific syntax, and if some invalid regex format is entered into the text entry, the query will fail.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda