update应该是mysql里最常用的sql之一了,在开发过程中总结了以下几种写法
1,简单类型
语句结构
update table set where
示例:
update product SET order_count = 2 WHERE id = 1
2,多表类型
语句结构
update table1,table2 set table1.col = table2.col where
示例:
update product as p,`order` as o SET p.id = o.product_id WHERE id = 1
3,子查询类型
语句结构
update table1,(select * from table) a set table1.col = a.col
示例:
UPDATE product as p,
( SELECT count( order_id ) AS order_count FROM `order` WHERE product_id = 1 ) AS o
SET p.order_count = o.order_count
WHERE
id = 1
网友评论