Re: Providing an alternative result when there is no result

From: Merlin Moncure <mmoncure(at)gmail(dot)com>
To: Joshua Berry <yoberi(at)gmail(dot)com>
Cc: Postgresql General Mailing list <pgsql-general(at)postgresql(dot)org>
Subject: Re: Providing an alternative result when there is no result
Date: 2009-05-19 18:08:20
Message-ID: b42b73150905191108l6e3307c8v5f54a91d4bcfe382@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Mon, May 18, 2009 at 3:13 PM, Joshua Berry <yoberi(at)gmail(dot)com> wrote:
> Hello all,
>
> Is there an easy and efficient way to return a boolean false for a query
> that returns no result, and true for one that does return a result?
>

Probably the best general approach is to:

select count(*) = 1 from
(
<query> limit 1
)q;

the point being that in some cases (not all obviously) the limit 1 can
be a huge win, as you only care if there are rows or not. with little
work (you have to be aware of if/when you can tack 'limit 1 onto a
query) you could generalize it into a pl/pgsql dynamic sql function
taking a query string.

merlin

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Richard Huxton 2009-05-19 18:38:46 Re: Locking to restrict rowcounts.
Previous Message James B. Byrne 2009-05-19 18:05:09 Help with join syntax sought