Re: select where true, or select where input = '$var'

From: Paul Jungwirth <pj(at)illuminatedcomputing(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: select where true, or select where input = '$var'
Date: 2015-02-19 20:51:54
Message-ID: 54E64CEA.3080407@illuminatedcomputing.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

> i want to select based on input, but if input is not provided or if
> input is empty, then i want to select all rows.

I think you can just use OR:

SELECT *
FROM table
WHERE (input = '' OR input = ?)

This is assuming that `input` is a column in your table and ? is the
user input, based on the query you provided. But are you sure that's
what you mean?

Also, if the `input` column can contain nulls you might also want:

SELECT *
FROM table
WHERE (input IS NULL OR input = '' OR input = ?)

Paul

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Chris Mair 2015-02-19 21:02:38 Re: rollback in C functions
Previous Message John R Pierce 2015-02-19 20:51:03 Re: select where true, or select where input = '$var'