pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Alvaro Herrera <alvherre@alvh.no-ip.org>
To: Shaozhong SHI <shishaozhong@gmail.com>
Cc: pgsql-general <pgsql-general@lists.postgresql.org>
Subject: Re: Merge into does not work
Date: Fri, 26 Nov 2021 16:44:36 -0300
Message-ID: <202111261944.siaofq7of32m@alvherre.pgsql> (raw)
In-Reply-To: <CA+i5JwZoUVr6CW4F2gCSUMjLHDXOrTWZ6=xwzsXwCjJtKnomtA@mail.gmail.com>

On 2021-Nov-26, Shaozhong SHI wrote:

> MERGE INTO Stock USING Buy ON Stock.item_id = Buy.item_id
>  WHEN MATCHED THEN UPDATE SET balance = balance + Buy.volume
>  WHEN NOT MATCHED THEN INSERT VALUES (Buy.item_id, Buy.volume);

It does work for me:

55479 15devel 680346=# MERGE INTO Stock USING Buy ON Stock.item_id = Buy.item_id
 WHEN MATCHED THEN UPDATE SET balance = balance + Buy.volume
 WHEN NOT MATCHED THEN INSERT VALUES (Buy.item_id, Buy.volume);
MERGE 2
Duración: 3,879 ms
55479 15devel 680346=# select * from stock;
 item_id │ balance 
─────────┼─────────
      20 │    1900
      10 │    3200
      30 │     300
(3 filas)

> I am using Postgres 9.6.

I am using the MERGE patch I posted here[1], on top of Postgres 15.

https://postgr.es/m/202111152245.byerxxac444d@alvherre.pgsql

-- 
Álvaro Herrera              Valdivia, Chile  —  https://www.EnterpriseDB.com/
"Pido que me den el Nobel por razones humanitarias" (Nicanor Parra)





view thread (7+ messages)  latest in thread

Message-ID: <202111261944.siaofq7of32m@alvherre.pgsql>
Permalink:  ../202111261944.siaofq7of32m@alvherre.pgsql/
Also on:    postgresql.org/message-id/202111261944.siaofq7of32m@alvherre.pgsql

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: alvherre@alvh.no-ip.org, shishaozhong@gmail.com
  Subject: Re: Merge into does not work
  In-Reply-To: <202111261944.siaofq7of32m@alvherre.pgsql>
  References: <CA+i5JwZoUVr6CW4F2gCSUMjLHDXOrTWZ6=xwzsXwCjJtKnomtA@mail.gmail.com> <202111261944.siaofq7of32m@alvherre.pgsql>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox