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

From Lars Kellogg-Stedman
Subject Default value if query returns 0 rows?
Date
Msg-id Pine.LNX.4.44.0409171056390.8060-100000@wolery.deas.harvard.edu
Whole thread Raw
Responses Re: Default value if query returns 0 rows?
Re: Default value if query returns 0 rows?
Re: Default value if query returns 0 rows?
List pgsql-general
Hello,

I have a simple two-column table mapping names to ids.  I'd like to write a
select statement that will return a default value if a given name isn't
found in the table.  That is, I want something equivalent to the following
pseudocode:

  if exists (select 1 from map where name = 'foo') then
    select id from map where name = 'foo'
  else
    select -1
  end if

I think I can see how to do this by writing a pl/pgsql function, but I'm
curious if it's possible to do this completely as part of a select
statement.  I've toyed with CASE expressions, but the fact that a missing
value returns 0 rows continues to foil me.

Thanks,

-- Lars

--
Lars Kellogg-Stedman <lars@oddbit.com>



pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: Converting varchar() to text
Next
From: Bruno Wolff III
Date:
Subject: Re: Default value if query returns 0 rows?