Re: SQL query - Mailing list pgsql-general

From Matt K
Subject Re: SQL query
Date
Msg-id 420C979A.8070109@kynx.org
Whole thread Raw
In response to SQL query  (David Goodenough <david.goodenough@btconnect.com>)
Responses Re: SQL query
List 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




pgsql-general by date:

Previous
From: John Sidney-Woollett
Date:
Subject: Re: vacuum confusion
Next
From: Janning Vygen
Date:
Subject: Re: SQL query