Showing posts with label Postgres. Show all posts
Showing posts with label Postgres. Show all posts

Sunday, 19 July 2020

copy column from one table to another without column duplicate postgres

if you want to update data to One table from another table without duplication of data.

//Postgres Database Query
insert into table1(column1,column2)
select c1,c2
  from table2 a
 where not exists ( select 0 from table1 b where b.id = a.id )


Wednesday, 31 July 2019

Find if a key exists or not - PostgreSQL JSONB Array


Find if a key exists or not - PostgreSQL JSONB Array


Records for data columns

Record 1:

[
  {    "KEY1": "VALUE1",    "KEY2": "VALUE2"  },
  {    "KEY1": "VALUE3",    "KEY3": "VALUE4"  },
]
Record 2:

[
  {    "KEY1": "VALUE1",    "KEY2": "VALUE2"  },
  {    "KEY3": "VALUE3",    "KEY4": "VALUE4"  },
]


create table fruits
(
  id serial not null,
  data jsonb
);

SELECT id, arr_elem
FROM fruits AS fruit, jsonb_array_elements(
    (
      SELECT data
      FROM test_table
      WHERE id = fruit.id
    )
) AS arr_elem
WHERE arr_elem#>'{KEY4}' IS NOT NULL

Tuesday, 30 July 2019

Postgres - where clouse in Jsonb Column


Sample JSON of column

   [{
"address": "Veshu",
"location": "SVNIT",
},{
    "address": "Palanpur Patiya",
"location": "Ganesh Mandir",
}]


SELECT * FROM customer WHERE addresses @> '[{"address": "Veshu"}]';