Re: What is the postgres version of mysql's "ON DUPLICATE KEY"

From: Gaetano Mendola <mendola(at)bigfoot(dot)com>
To: Nick <nboutelier(at)hotmail(dot)com>
Subject: Re: What is the postgres version of mysql's "ON DUPLICATE KEY"
Date: 2004-09-10 22:02:26
Message-ID: 41422472.6040101@bigfoot.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Nick wrote:

> I have a table with columns
> (product_id,related_product_id,related_counter)
>
> If product A is related to product B then a record should be created,
> if the record already exists then the related_counter should be
> incremented.
>
> This is very easy to do with MySQL using INSERT... ON DUPLICATE KEY.
> Standard or not, it is very usefull.
>
> Is there a way to catch the insert error. For example...
>
> INSERT INTO related_products (product_id,related_product_id) VALUES
> (?,?);
> IF (???error: duplicate key???) THEN
> UPDATE related_products SET related_counter = related_counter + 1;
> END IF;
>
> -Nick

With a rule you can do it easily ( never tried ).

Regards
Gaetano Mendola

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Bruce Momjian 2004-09-10 22:08:20 Re: 8.0.0beta2: Ownership of implicit sequences after dump/restore
Previous Message Tom Lane 2004-09-10 21:55:21 Re: Speeding up LIKE with placeholders?