执行mysql update,或者delete的时候会遇到:

相关的原因自不必说:下面有stackoverflow中的帖子:

https://stackoverflow.com/questions/4429319/you-cant-specify-target-table-for-update-in-from-clause

我采取的是将id ,放到一个临时表中;

(2):https://stackoverflow.com/questions/45494/mysql-error-1093-cant-specify-target-table-for-update-in-from-clause

   添加select:

    

UPDATE tbl SET col = (
  SELECT ... FROM (SELECT.... FROM) AS x);

    这里面讲述了,mysql 5.7以上添加select 还是不好用,

However, beware that from MySQL 5.7.6 and onward, the optimiser may optimise out the subquery, and still give you the error. Luckily, the optimizer_switch variable can be used to switch off this behaviour; although I couldn't recommend doing this as anything more than a short term fix, or for small one-off tasks.

SET optimizer_switch = 'derived_merge=off';

Thanks to Peter V. Mørch for this advice in the comments.

Example technique was from Baron Schwartz, originally published at Nabble, paraphrased and extended here.

 

(3);执行带索引:

    1\  update t set t.sex = 'm' where t.id in (select id from d);   //explain 感觉很慢;

   正确做法:

    update t,d set t.sex = 'm'where t.id = d.id; //soso..

相关文章:

  • 2021-04-10
  • 2021-08-11
  • 2021-12-09
  • 2022-02-02
  • 2022-12-23
  • 2022-12-23
  • 2021-12-04
猜你喜欢
  • 2022-12-23
  • 2021-10-28
  • 2022-12-23
  • 2021-11-08
  • 2022-02-12
  • 2021-06-28
  • 2021-11-21
相关资源
相似解决方案