本文以t_product,t_model,t_user三个表为例来说明:
目标:将满足条件的t_model.version从2014更新为2015
查询条件:t_user.name='test' t_model.version=2014
查询结果为t_model.id
SELECT m.id from t_model m,t_product p,t_user u where m.pid=p.id and p.uid=u.id and u.name='test' and m.version=2014
因为mysql中You can't specify target table 't_model' for update in FROM clause错误的意思是说,不能先select出同一表中的某些值,再update这个表(在同一语句中)。 采用下述方法解决
UPDATE t_model m INNER JOIN t_product p ON m.pid=p.id INNER JOIN t_user u ON p.uid=u.id SET m.version=2015 WHERE u.name='test' and m.version=2014