Mysql 使用maxdate更新行,其中提供了select中的id和select中的maxdate
我有一个select语句,它返回一组ID和一个maxdate。现在,我想用给定的maxdate更新invoices表中的performance_date,其中给定的invoices ID为Mysql 使用maxdate更新行,其中提供了select中的id和select中的maxdate,mysql,sql-update,Mysql,Sql Update,我有一个select语句,它返回一组ID和一个maxdate。现在,我想用给定的maxdate更新invoices表中的performance_date,其中给定的invoices ID为 Select invoices.id, max(invoicepositions.performance_date) as maxdate from invoices inner join invoicepositions on invoices.id = invoicepositions.invoice_i
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中实现这一点?您可以使用当前的
选择查询作为一个查询,并使用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