Sql Query To Update Multiple Rows Using Single Update Statement
If you want to update multiple rows in a table based on conditions involving multiple tables then use the following SQL query:
UPDATE suppliers
SET supplier_name = ( SELECT customers.name
FROM customers
WHERE customers.customer_id = suppliers.supplier_id)
WHERE EXISTS
( SELECT customers.name
FROM customers
WHERE customers.customer_id = suppliers.supplier_id);
So, whenever a supplier_id matched a customer_id value, the supplier_name would be overwritten to the customer name from the customers table.
If you want to update multiple rows in a table based on conditions involving multiple tables then use the following SQL query:
UPDATE suppliers
SET supplier_name = ( SELECT customers.name
FROM customers
WHERE customers.customer_id = suppliers.supplier_id)
WHERE EXISTS
( SELECT customers.name
FROM customers
WHERE customers.customer_id = suppliers.supplier_id);
So, whenever a supplier_id matched a customer_id value, the supplier_name would be overwritten to the customer name from the customers table.
No comments:
Post a Comment