Re: Question - Query based on WHERE OR

From: "John D(dot) Burger" <john(at)mitre(dot)org>
To: pgsql general <pgsql-general(at)postgresql(dot)org>
Subject: Re: Question - Query based on WHERE OR
Date: 2007-01-11 22:42:29
Message-ID: 34BFD047-9BE6-4841-A174-01B410A6F436@mitre.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Mike Poe wrote:

> SELECT foo, baz, bar FROM public.table WHERE lastname ~*
> '$lastname' OR ssn='$ssn'"
>
> I need to leave the last name a wildcard in case someone enters a
> partial name, lower case / upper case, etc.
> I want the SSN to match exactly if they search by that.
> The way it's written, if I enter nothing for the last name, it
> returns all rows, regardless of what I enter for the ssn.
> I understand why it happens, but what can I do to return the
> desired results?

How about:

SELECT foo, baz, bar FROM public.table
WHERE ('$lastname' != '' and lastname ~* '$lastname') OR ssn='$ssn'";

Or perhaps some more general condition for "empty" lastname parameters.

- John Burger
MITRE

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Andrew Dunstan 2007-01-11 23:04:56 Re: [HACKERS] Checkpoint request failed on version 8.2.1.
Previous Message Simon Riggs 2007-01-11 22:39:00 Re: pg_standby testing notes