Re: INSERT only under certain conditions (SELECT)

From: Bruno Wolff III <bruno(at)wolff(dot)to>
To: Joachim Trinkwitz <jtr(at)uni-bonn(dot)de>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: INSERT only under certain conditions (SELECT)
Date: 2002-07-08 12:40:13
Message-ID: 20020708124013.GA30022@wolff.to
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Mon, Jul 08, 2002 at 03:18:33 +0200,
Joachim Trinkwitz <jtr(at)uni-bonn(dot)de> wrote:
> Hi,
>
> I want to insert a row only under condition that there isn't already
> another row with similar values -- something like a INSERT INTO
> ... WHERE NOT EXISTS (SELECT ...)?

You can do something like the following:
insert into tab (col1, col2, col3) select 'val1', 'val2', 'val3' where
not exists (select * from tab where col1 = 'val1' and col2 = 'val2' and
col3 = 'val3');

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Christoph Haller 2002-07-08 12:54:00 Re: Double quotes?
Previous Message Archibald Zimonyi 2002-07-08 10:58:51 Re: Double quotes?