-1
Tables
Contract
id, contract, credit_id, debtor, evento_id, campanha_id
Title
id, contrato_id, title, maturity, value
The idea is to update the field campanha_id
of contracts using a sub-allotment and Inner Join
The question is: How to update the campanha_id field of all contracts returned in the query ?
I built a complete example with query parameters on SQL Fiddle for better viewing of tables.
No update query:
SELECT c.id,
c.contrato,
t.valor_total
FROM contrato c
INNER JOIN(SELECT contrato_id,
Sum(valor) valor_total
FROM titulo t
GROUP BY contrato_id) t
ON t.contrato_id = c.id
WHERE c.credor_id = 2
Query that returns syntax error
UPDATE c
SET c.campanha_id = 5
FROM contrato AS c
INNER JOIN (SELECT contrato_id,
Sum(valor) valor_total
FROM titulo t
GROUP BY contrato_id) t
ON t.contrato_id = c.id
WHERE c.credor_id = 2