I have a database with following tables an relationships
- Requests
- Responses
- Payments
A payment can have multiple requests (one-to-many).
A request has one response (one-to-one).
A payment also has a field called SuccessfulResponseId
which is the ID of a request which succeeds. (To be explicit, the response table does not contain a direct reference to the payments table, only an indirect one via the requests table)
I have an update SQL query which looks something like this:
update "Responses"
set "Status" = correct_response."Status"
from (
select "Status",
from "Responses"
where "Responses"."Id" in (select "PaymentResponseId"
from "Requests"
where "Status" = 4
and "PaymentId" = '123456')
) correct_response
where "Id" in (Select "SuccessfulResponseId"
from "Payments"
where "Payments"."Id" = '123456')
In this case, the table schema contains two tables Requests
and Responses
. One request can have multiple responses.
The purpose of the update query above is to copy some data from one row in the response table to another row in the response table (where all responses belong to the same payment --- indirectly via requests tables)
Now, I have a bunch of Payments for which I want to run the query. Is there a SQL way to run this query for different Payment Ids. something like
-- I understand the following is not a valid syntax;
-- using it to just make the requirement more visual
(
update "Responses"
set "Status" = correct_response."Status"
from (
select "Status",
from "Responses"
where "Responses"."Id" in (select "PaymentResponseId"
from "Requests"
where "Status" = 4
and "PaymentId" = X)
) correct_response
where "Id" in (Select "SuccessfulResponseId"
from "Payments"
where "Payments"."Id" = X)
) Where X in (select Id from Payments where ...some condition...)