我有一张桌子,比如说,桌子 list
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 |
我试过:
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
说明:我正在try 插入randomCol1和next\u id,其中next\u id需要从给定的unique\u id中 Select .因此,有一个子查询.我想用插入同一查询后返回的任何unique\u id更新给定unique\u id的下一个\u id.