I've got a select statement that joins a couple of tables and grabs some information. I'd like to updated all of the records on one of those tables (found in the select) with information contained in the select. The select looks like this:
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;
In english, a document can belong to customers and accounts. I'm looking for the account records that match customer records where the document belongs to the customer, but not the account.
Now, I can manually go through the results and run this:
UPDATE document
SET accountid = /*account.id*/
WHERE id = /*document.id*/;
which works as I would like, but there's a decent amount of records that match my query and I'd like to do it in a single statement if I could.
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;
That should do it all in one go
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With