Re: Default value if query returns 0 rows? - Mailing list pgsql-general

From Edmund Bacon
Subject Re: Default value if query returns 0 rows?
Date
Msg-id tmE2d.435990$M95.339884@pd7tw1no
Whole thread Raw
In response to Default value if query returns 0 rows?  (Lars Kellogg-Stedman <lars@oddbit.com>)
List 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?

pgsql-general by date:

Previous
From: Lars Kellogg-Stedman
Date:
Subject: Re: Default value if query returns 0 rows?
Next
From: Peter Eisentraut
Date:
Subject: Re: How to find a column name