Re: INSERT WHERE NOT EXISTS

From: Lincoln Yeoh <lyeoh(at)pop(dot)jaring(dot)my>
To: Karsten Hilbert <Karsten(dot)Hilbert(at)gmx(dot)net>, pgsql-general(at)postgresql(dot)org
Subject: Re: INSERT WHERE NOT EXISTS
Date: 2003-06-28 04:00:41
Message-ID: 5.2.1.1.1.20030628114112.02ec7d50@mbox.jaring.my
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

At 01:36 AM 6/28/2003 +0200, Karsten Hilbert wrote:

>Because
>
> >>select exists(select * from tablename where [cond])
>
>will return after finding 1 matching row (or am I
>wrong ?) while
>
> > SELECT 1 FROM tablename WHERE [cond];
>
>will return a 1 for *each* matching row (just tested).

You can use limit. Postgresql is smart enough to stop once it has the
necessary rows[1].

select 1 from table name where cond limit 1;

Link.

[1] Or was it necessary rows + 1? Agh, something wrong with my memory.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2003-06-28 04:17:26 Re: Redhat's "enhancements" to PG
Previous Message Ron Johnson 2003-06-28 03:50:58 Re: Redhat's "enhancements" to PG