Re: SQL query

From: Matt K <matt(at)kynx(dot)org>
To: David Goodenough <david(dot)goodenough(at)btconnect(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: SQL query
Date: 2005-02-11 11:31:38
Message-ID: 420C979A.8070109@kynx.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

David Goodenough wrote:

>I could do this by doing a select * from addresses where customer = ?
>and type = 'billing', looking to see if there is a result row and if not
>repeating the query with type = 'default', but that seems inelegant to
>me.
>
>
Use NULL to indicate that the customer type is default. Then you can
query with:

select * from addresses where customer = ?
and coalesce(type, 'billing') = 'billing'

If type is NULL, the comparison will be 'billing' = 'billing' - always true. If there's a bunch of non-null type addresses, you'll get the 'billing' one.

http://www.postgresql.org/docs/8.0/interactive/functions-conditional.html#AEN12003

Matt

In response to

  • SQL query at 2005-02-11 11:07:24 from David Goodenough

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Janning Vygen 2005-02-11 11:41:06 Re: SQL query
Previous Message John Sidney-Woollett 2005-02-11 11:13:17 Re: vacuum confusion