MySQL: Update all rows in a table matching results of another query

后端 未结 3 1751
一个人的身影
一个人的身影 2021-02-04 05:56

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

3条回答
  •  有刺的猬
    2021-02-04 06:24

    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.

提交回复
热议问题