G
G
GDim2016-03-14 08:41:04
PostgreSQL
GDim, 2016-03-14 08:41:04

Postgresql how to update existing array values?

There is a Postgresql chat_channels table with a users integer[] NOT NULL DEFAULT ARRAY[]::integer[] column
. It stores user id arrays.
How to replace the id of a certain user with the id of another in all rows of the table where this id is? The rest of the array elements should remain unchanged.
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Melkij, 2016-03-14
@GDim

update tablename set users = array_replace(users, :from, :to) where users @> array[:from]

It may be necessary to add explicit type casting, depending on how the parameters will be substituted.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question