当使用mysql条件更新时--最先让人想到的写法
UPDATE buyer SET is_seller=1 WHERE uid IN (SELECT uid FROM seller)
此语句是错误的,会报错 You can't specify target table 'xxx' for update in FROM
这是因为:
mysql的update的一些特点
1、update 时,更新的表不能在set和where中用于子查询;
2、update 时,可以对多个表进行更新(sqlserver不行);
如:update ta a,tb b set a.Bid=b.id ,b.Aid=a.id;
3、update 后面可以做任意的查询,这个作用等同于from;
正确的方式是,例:
简单的更新:
UPDATE roles_permissions
a SET a.roles_id=89 WHERE a.roles_name
='0|||管理员'
较为复杂的更新:
例1:
UPDATE order_mall a,(SELECT order_mall.id FROM order_mall
,order_goods
WHERE order_mall.id
=order_goods.order_id
AND order_goods.order_status=8 AND order_goods.order_goods_type=3) b
SET a.status
=4
WHERE a.id=b.id
例2:
UPDATE core_user
a,(SELECT message_push_conf.user_id,message_push_conf.open_push
FROM message_push_conf
) b
SET a.switch_push
=b.open_push
WHERE a.id=b.user_id