I have two tables in a database. First table is Book with columns Id(varchar(40)), "Name"(varchar(255)), Tags(varchar(40)[]). Second table Temp with columns "Name"(varchar(255)), Id(varchar(40)), duplicateids (varchar(40)[]).
I need to write script in postgresql. If any record from the Book table in the Tags column has an id that matches the element in the duplicateids column of the Temp table, then replace only this element in the array in the Tags column from the Book table with the Id field from the table Temp.
Example Table "Book"
| Id | Name | Tags |
|---|---|---|
| '1' | 'Book1' | ['tag1', 'tag2', 'tag3'] |
| 2' | 'Book2' | ['tag4', 'tag5'] |
Table "Temp"
| Name | Id | duplicateids |
|---|---|---|
| 'Temp1' | 'testtag' | ['tag2', 'tag5'] |
| 'Temp2' | 'tag100' | ['tag3'] |
After the script should be:
| id | Name | Tags |
|---|---|---|
| '1' | 'Book1' | ['tag1', 'testtag', 'tag100'] |
| '2' | 'Book2' | ['tag4', 'testtag5'] |