From: | Sam Mason <sam(at)samason(dot)me(dot)uk> |
---|---|
To: | pgsql-general(at)postgresql(dot)org |
Subject: | Re: Select CASE when null ? |
Date: | 2009-01-15 11:46:46 |
Message-ID: | 20090115114646.GA3008@frubble.xen.chris-lamb.co.uk |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
On Wed, Jan 14, 2009 at 03:56:25PM -0500, Mark Styles wrote:
> SELECT COALESCE(mid,0) AS mid, COALESCE(id_group,0) AS id_group
> FROM users
> WHERE username = 'test'
> UNION
> SELECT 0, 0
> WHERE NOT EXISTS (SELECT 1 FROM users WHERE username = 'test');
An alternative using outer joins would be:
SELECT COALESCE(mid,0) AS mid, COALESCE(id_group,0) AS id_group
FROM (SELECT 1) x
LEFT JOIN users ON username = 'test';
Unions tend to preclude various optimisations so I'd tend to stay away
from them where possible. This query will also only perform only one
index scan of users, rather than two.
--
From | Date | Subject | |
---|---|---|---|
Next Message | Merlin Moncure | 2009-01-15 12:52:33 | Re: fire trigger for a row without update? |
Previous Message | Sam Mason | 2009-01-15 11:33:09 | Re: Query question |