Search Postgresql Archives

Re: Select CASE when null ?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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.

-- 

  Sam  http://samason.me.uk/

-- 
Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux