mysql联合其他表做更新

在sql server中,我们可是使用以下update语句对表进行更新:

update a set a.xx= (select yy from b) where a.id = b.id ;

但是在mysql中,不能直接使用set select的结果,必须使用inner join

UPDATE tt_vmap_connect_doc d INNER JOIN (
SELECT 
if(ct.vehicle_site_distance IS not NULL,
            if(ct.vehicle_site_distance > 1000,2,1),
            if(LOCATE('xx',rd.vehicle_number) > 0,1,
                if(LOCATE('yyyy',rd.fault_names) > 0,1,2)
            )
) as gps,
ct.id 
FROM tt_vmap_connect_doc ct
INNER JOIN tt_vmap_order_doc od ON ct.order_id = od.id
INNER JOIN tt_vmap_repair_doc rd ON rd.id = od.repair_id
) c ON c.id = d.id SET d.gps_coincide = c.gps,d.reference_value = 1000;

 

另外mysql更新多个字段也不能用括号确定多个字段,而必须一个一个set

 

update a set(a.province,a.city)=(select province,city from b where b.mobile=a.mobile)  --不可用于mysql

update a set a.province=b.province,a.city=b.city from a inner join b on a.mobile=b.mobile.
或者update a set a.province=b.province,a.city=b.city from a,b where a.mobile=b.mobile.

 

posted @ 2019-02-15 11:35 风吹过的绿洲 阅读(...) 评论(...) 编辑 收藏