I am making an application which optimizes routes (kind of like a VRP). The user must be able to copy all input data, and make changes to this data. This way, they are able to find out what the impact is on the optimization.
Therefore I chose to let them be able to copy all input data, and have sort of like a version management of the data.
I want to copy the following tables:
TableOne
ID UUID WEIGHT CODE
1 abc 15 AB
2 abd 5 AC
TableTwo
ID UUID SIZE TABLE1_FK
1 abe 1 1
2 abf 3 2
The resulting tables (after copying):
TableOne
ID UUID WEIGHT CODE
1 abc 15 AB
2 abd 5 AC
3 abg 15 AB
4 abh 5 AC
TableTwo
ID UUID SIZE TABLE1_FK
1 abe 1 1
2 abf 3 2
3 abi 1 1
4 abj 3 2
Up to this point, I can manage. But now when I want to update the foreign keys of TableTwo to point to the copied lines in TableOne, I get stuck.
I want to do something like:
UPDATE TableOne
SET TABLE1_FK =
(SELECT t.ID
FROM TableOne t
WHERE t.WEIGHT = (SELECT t.WEIGHT
FROM TableOne t1
WHERE ...)
)
And this is where I get stuck. The wanted result is:
TableTwo
ID UUID SIZE TABLE1_FK
1 abe 1 1
2 abf 3 2
3 abi 1 3
4 abj 3 4
Any suggestions?