Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

191
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda