一条update语句更新多条sql记录

要求:将dispatch表中关联的用户名更新到dispatch表中。

UPDATE xn_m_dispatch disp
LEFT JOIN
(SELECT dis.`id` AS dispatchID,u.`name` AS userName FROM xn_m_dispatch dis
LEFT JOIN xn_m_logistics_order lo ON lo.`id`=dis.`logistics_order_id`
LEFT JOIN xn_m_company com ON lo.`carry_company_id`=com.`id`
LEFT JOIN xn_m_user u ON u.`id`=com.`company_admin`) AS t ON t.dispatchID=disp.id
SET disp.auditor=t.userName WHERE disp.id=t.dispatchID;

你可能感兴趣的:(sql)