0

Column values in DB are something like this {1,2,4} & the value which I want to edit is currently null.

What postgres I can execute to make it similar to other value.

I've run a query but it has given undesired result: Result-> {[0:0]={1}} The query I've executed

update table_name set column_name[0]=1 where unique_val = 1;

enter image description here

After executing the query , I want row2-> value to get in similar structure to first row.

Any help would be appreciated.

7
  • 2
    For me it's completely not clear what you want to achieve. Please show us some sample data and the expected output. Commented Feb 25, 2021 at 11:31
  • I've added more description, see if it's sufficient Commented Feb 25, 2021 at 11:44
  • What is the expected output after your update? Commented Feb 25, 2021 at 11:45
  • {1,2,3,4} or same as row 1 Commented Feb 25, 2021 at 11:46
  • 1
    {1,2,3,4} is not the same as row 1. Anyway can you say why this is not sufficient: UPDATE table_name SET column_name = '{1,2,3,4}' WHERE id = 2? Commented Feb 25, 2021 at 11:47

1 Answer 1

1

demo:db<>fiddle

Simply updating with {1,2,3,4}:

UPDATE table_name
SET val = '{1,2,3,4}'
WHERE id = 2

Updating with the first record's value:

UPDATE table_name t
SET val = s.val
FROM (
    SELECT val FROM table_name WHERE id = 1
) s
WHERE t.id = 2

Addionally:

Please don't store array in your table. This makes everything much more complicated (searching and updating specific array elements) and less performant (joins on array elements require an unnest operation, indexing is not simply possible, ...). Please normalize your table design. That means there should be a relation table with columns table_name_id and array_element_id.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.