MySQL 从一张表update字段到另外一张表中
先来几个简单的示例
Solution1: 1列
updatestudents,cityc sets.city_name=c.name wheres.city_code=c.code;
Solution2: 多个列
updatea,b seta.title=b.title,a.name=b.name wherea.id=b.id
Solution3:子查询
updatestudentssetcity_name=(selectnamefromcitywherecode=s.city_code);
我们再来看几个负责写的
例如:把表tk_zyt_scenery_order的字段更新到t_advs_order中去,一般可能会这样写:
UPDATEt_advs_orderSET attribute1=(SELECTo.order_stateFROMtk_zyt_scenery_orderoWHEREo.order_id=`on`), attribute2=(SELECTo.order_stateFROMtk_zyt_scenery_orderoWHEREo.order_id=`on`) WHEREEXISTS(SELECTo.order_stateFROMtk_zyt_scenery_orderoWHEREo.order_id=`on`);
这样效率比较低下,优化写法:
UPDATEt_advs_orderaINNERJOINtk_zyt_scenery_ordersONs.order_id=a.`on`SET a.attribute1=s.order_id, a.attribute2=s.order_id;