Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.databases.postgresql > #814

Re: Update Json column

From Laurenz Albe <laurenz@nospam.pn>
Newsgroups comp.databases.postgresql
Subject Re: Update Json column
Date 2018-03-07 07:48 +0000
Organization A noiseless patient Spider
Message-ID <p7o5gf$g10$1@dont-email.me> (permalink)
References (1 earlier) <p73l09$27o$1@dont-email.me> <c9fb27ea-edc5-44db-8bb4-92b262192d47@googlegroups.com> <p78k0s$ani$1@dont-email.me> <23a909c9-9a4e-462a-89fa-a1efda4fb198@googlegroups.com> <efaf09da-16e0-48ff-b4eb-5401cd6be4cd@googlegroups.com>

Show all headers | View raw


forough m wrote:
>> > >>> I create a table with a Json type column. I need to update one
>> > >>> key:value in Json. i.e increment value of 3 to 5. Can i do it?
>> > >> 
>> > >> If you have PostgreSQL 9.5 or better, you can use "json_set" like
>> > >> this:
>> 
>> Worked. Thanks :) But, why it remove all fields if {b,c} not exist?
>> I want to insert it if not exist
> 
> I want to say: If 'f' exist, then increment it +3 else, if not exist
> create it with value = 0

You could use CASE:

UPDATE t SET j = CASE WHEN <j contains key>
                      THEN <j with 3 added>
                      ELSE <j with new key>
                 END

Back to comp.databases.postgresql | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Update Json column forough m <fmirkarimzade@gmail.com> - 2018-02-26 22:09 -0800
  Re: Update Json column Laurenz Albe <laurenz@nospam.pn> - 2018-02-27 13:04 +0000
    Re: Update Json column forough m <fmirkarimzade@gmail.com> - 2018-02-28 03:06 -0800
    Re: Update Json column forough m <fmirkarimzade@gmail.com> - 2018-02-28 03:21 -0800
      Re: Update Json column Laurenz Albe <laurenz@nospam.pn> - 2018-03-01 10:18 +0000
        Re: Update Json column forough m <fmirkarimzade@gmail.com> - 2018-03-05 01:44 -0800
          Re: Update Json column forough m <fmirkarimzade@gmail.com> - 2018-03-06 07:47 -0800
            Re: Update Json column Laurenz Albe <laurenz@nospam.pn> - 2018-03-07 07:48 +0000
              Re: Update Json column forough m <fmirkarimzade@gmail.com> - 2018-03-07 00:09 -0800
                Re: Update Json column Laurenz Albe <laurenz@nospam.pn> - 2018-03-08 11:36 +0000
                Re: Update Json column forough m <fmirkarimzade@gmail.com> - 2018-03-09 21:07 -0800
              Re: Update Json column forough m <fmirkarimzade@gmail.com> - 2018-03-07 00:10 -0800

csiph-web