Answer the question
In order to leave comments, you need to log in
How to select the last occurrence that satisfies a query?
Hello, please tell me how to solve the following problem.
There is this request:
UPDATE
sales as s
join payments_parts pp on pp.FK_sale = s.ID
join payments pay ON pp.FK_payment = pay.id
SET s.plan_payment_date = pay.payment_date
WHERE s.ID = pp.FK_sale and pay.ID = pp.FK_payment;
Answer the question
In order to leave comments, you need to log in
If it will ever be useful to someone, then here is a request that satisfies the condition.
UPDATE sales AS s
JOIN ( SELECT pp.FK_sale, MAX(pay.payment_date) payment_date
FROM payments_parts pp
JOIN payments pay ON pp.FK_payment = pay.id
GROUP BY pp.FK_sale) p ON p.FK_sale = s.ID
SET s.plan_payment_date = p.payment_date;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question