mysql DELETE NOT IN 删除问题 You can‘t specify target table ‘basic_calculation_substance‘ for update in

You can’t specify target table ‘basic_calculation_substance’ for update in FROM clause

When mysql performs deletion, there is a certain verification in not in.

I have stepped on this trap twice and always forget it every time, so I will record it here.

  delete from tableA where id not in123

There is no problem with this

	delete from tableA where id not inselect id from tableB)

There is no problem

But, if you write like this, there will be a problem

delete from tableA where id not in (select id from tableA )

So how to solve it? In fact, this is the specification of MySQL. You cannot perform delete query operations on the same table at the same time. You must create an alias to cheat the verification.

Solution

 delete from tableA ( select * from (select id from tableA) As t)

Guess you like

Origin blog.csdn.net/csl12919/article/details/130971006