From: | Mark Styles <postgres(at)lambic(dot)co(dot)uk> |
---|---|
To: | pgsql-general(at)postgresql(dot)org |
Subject: | Re: Select CASE when null ? |
Date: | 2009-01-14 20:59:32 |
Message-ID: | 20090114205932.GK18936@lambic.co.uk |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
On Wed, Jan 14, 2009 at 05:35:55PM -0300, Camilo Sperberg wrote:
> SELECT
> CASE mid WHEN NULL THEN CAST(0 AS integer)
> ELSE mid
> END AS mid,
> CASE id_group WHEN NULL THEN CAST(0 AS integer)
> ELSE id_group
> END AS id_group
> FROM users
> WHERE username = 'test';
>
> This query returns:
> mid --- id_group
> 1 --- 2
>
> Now, what I want is when the user isn't found, (aka WHERE username isn't
> found) it should return me this:
>
> mid --- id_group
> 0 --- 0
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');
--
Mark
http://www.lambic.co.uk
From | Date | Subject | |
---|---|---|---|
Next Message | Camilo Sperberg | 2009-01-14 21:23:10 | Re: Select CASE when null ? |
Previous Message | Mark Styles | 2009-01-14 20:56:25 | Re: Select CASE when null ? |