gpt4 book ai didi

mysql - 使用 maxdate 更新行,其中给出了 id from select 和 maxdate from select

转载 作者:行者123 更新时间:2023-11-29 02:40:33 28 4
gpt4 key购买 nike

我有一个返回一组 id 和一个 maxdate 的 select 语句。现在,我想用给定发票 ID 的给定最大日期更新发票表中的 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 可以做到这一点?

最佳答案

您可以将当前的 SELECT 查询用作 Derived Table并使用id将其加入invoices表,然后更新。

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

关于mysql - 使用 maxdate 更新行,其中给出了 id from select 和 maxdate from select,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53453187/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com