I need to make the connection with the PK from a table to its correspondent FK from another table. This is my db:
I only have the case_id
from the case
table, and I need the case_uid
from the case_test
table for this SQL statement:
UPDATE case_test (this part is ok)
SET country = 'Canada' (this part is ok)
WHERE case_test.case_uid = case.case_uid (is the same as case_uid from the case table but i only know the case_id field from that table)
How can I make the connection between the keys knowing that I only know case_id?
2
Answers
DISCLAIMER: Use at your own risk!
Ensure you have a backup before executing the UPDATE statement below.
Check the inner EXISTS SELECT statement before applying the update.
Use a scalar subquery to extract
case_uid
from tablecase
.Btw. could it be that there is more than one
case_uid
for acase_id
in tablecase
? If so then the subquery is not scalar anymore and thewhere
clause shall usein
operator instead of=
Unrelated but
case
is not a proper name for a table.