Re: Default value if query returns 0 rows?

From: Edmund Bacon <ebacon(at)SpamMeNot(dot)onesystem(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Default value if query returns 0 rows?
Date: 2004-09-17 16:34:33
Message-ID: tmE2d.435990$M95.339884@pd7tw1no
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Tom Lane wrote:

> Is the name unique? If so you could do
>
> select * from
> (select id from map where name = 'foo'
> union all
> select -1) ss
> limit 1;
>
>
> Another way is a subselect:
>
> select coalesce((select id from map where name = 'foo'), -1);
>
> but this one will actively blow up if there are multiple 'foo' rows,
> so it doesn't solve that problem either.

Can't you just:

select coalesce(id, -1) from map where name = 'foo' ?

Or am I missing something?

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Daniele Beauquier 2004-09-17 16:50:53 Import an Excel table to a Postgresql one
Previous Message John Sidney-Woollett 2004-09-17 16:26:50 Re: psql + autocommit