我有一条select语句,该语句返回一组ID和一个maxdate。现在,我要使用给定的发票ID的给定的maxdate更新发票表中的performance_date。
Select invoices.id, max(invoicepositions.performance_date) as maxdate
from invoices
inner join invoicepositions on invoices.id = invoicepositions.invoice_id
where invoices.performance_date IS NULL
group by invoices.id
(MySQL)怎么可能?
答案 0 :(得分:1)
您可以将当前的SELECT
查询用作Derived Table,并使用Join
将其invoices
到id
表中,然后进行更新。
UPDATE invoices AS i
JOIN
(
Select invoices.id, max(invoicepositions.performance_date) as maxdate
from invoices
inner join invoicepositions on invoices.id = invoicepositions.invoice_id
where invoices.performance_date IS NULL
group by invoices.id
) AS dt
ON dt.id = i.id
SET i.performance_date = dt.maxdate