I\'ve written a query returning rows associating Customers and Salespeoeple.
Note that the query joins several database tables. And note that not all c
Most widely supported option
UPDATE INVOICES
SET s_id = (SELECT cs.s_id
FROM CUSTOMERS_AND_SALES cs
WHERE cs.c_id = INVOICES.c_id),
s_name = (SELECT cs.s_name
FROM CUSTOMERS_AND_SALES cs
WHERE cs.c_id = INVOICES.c_id)
WHERE INVOICES.c_id IN (SELECT cs.s_id
FROM CUSTOMERS_AND_SALES cs)
UPDATE INVOICES
JOIN CUSTOMERS_AND_SALES cs ON cs.c_id = INVOICES.c_id
SET s_id = cs.s_id,
s_name = cs.s_name
Assuming your first table is named customers
and those customers without a salesperson have an s_id
of NULL
UPDATE invoices JOIN customers USING (c_id)
SET invoices.s_id = customers.s_id, invoices.s_name = customers.s_name
WHERE customers.s_id IS NOT NULL;
I suggest testing in development or running a SELECT
query using the JOIN
above first to ensure the results.
You can create a view to make your UPDATE
statement simple. The view should contain your query (in your case the query that associates customers and salespeople). Then update your table (invoices
in your case) like this:
update TableToUpdate ttu, MyView mv
set ttu.column = mv.column
where ttu.key = mv.key