I have a table, let's say, tableList
| unique_id | next_id | randomCol1 |
|---|---|---|
| 4 | 5 | string 1 |
| 5 | 6 | string 2 |
| 6 | 0 | string 3 |
Where unique_id is auto increment column and next_id is the unique_id of the row it is pointing to. At present we have 4->5->6->0(0 is a Non existing row).
Now I want to do an insertion of a row next to a given unique_Id, Let's say next to unique_Id=4.
At the time of insertion I want to extract the next_id(5) of the given unique_id(here, 4). The row which I'm about to insert will have next_id = 5 and row with unique_id = 4 will update its next_id to 7 (or whatever unique_id was generated at the time of insertion)
So, My table will look like : 4->7->5->6->0
| unique_id | next_id | randomCol1 |
|---|---|---|
| 4 | 7 | string 1 |
| 5 | 6 | string 2 |
| 6 | 0 | string 3 |
| 7 | 5 | string 4 |
I tried :
Insert into tableList(randomCol1,next_id)
values('string 4', select next_id from tableList where unique_id=4) returning unique_id as current_ID
Update tableList set next_id = current_id where unique_id=4
Explanation : I'm trying to insert randomCol1 and next_id where next_id needs to be selected from given unique_id. Hence, a sub query for that. I want to update the next_id of given unique_id with whatever unique_id was returned after insertion in same query. But My subquery and update in the same query with returning id is not working.