SQL - 如何在给定select的结果的情况下批量更新

Ste*_*ers 4 mysql sql

我有一个select语句,它连接几个表并获取一些信息.我想用select中包含的信息更新其中一个表(在select中找到)的所有记录.选择如下:

SELECT  account.id
        document.id
FROM    customer INNER JOIN account ON
            (customer.firstname = account.firstname AND 
            customer.lastname = account.lastname AND
            customer.phone = account.phone)
        INNER JOIN document ON
            customer.id = document.customerid
WHERE   document.accountid IS NULL;
Run Code Online (Sandbox Code Playgroud)

在英语中,文档可以属于客户和帐户.我正在寻找与客户记录匹配的帐户记录,其中文档属于客户,而不是帐户.

现在,我可以手动完成结果并运行:

UPDATE  document
SET     accountid = /*account.id*/
WHERE   id = /*document.id*/;
Run Code Online (Sandbox Code Playgroud)

它可以按照我的意愿运行,但是有大量的记录符合我的查询,如果可以的话,我想在一个声明中完成.

Mat*_*t S 6

UPDATE document, account, customer
SET documnet.accountid = account.id
WHERE (customer.firstname = account.firstname AND customer.lastname = account.lastname AND customer.phone = account.phone)
AND customer.id = document.customerid
AND document.accountid IS NULL;
Run Code Online (Sandbox Code Playgroud)

这应该一气呵成