本文轉自:91博客;原文地址:http://www.9191boke.com/644523640.html
當使用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
