mysql DELETE NOT IN 删除问题

已经踩过两次这个坑了,每次总忘,特此记录下

这是mysql 比较恶心的地方

  delete from tableA where id not in123) 
  这个是没有问题的
	delete from tableA where id not inselect id from tableB)
	也是木有问题的

但是。。。。。。。。下面这个就会出现问题

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

那么怎么解决呢,其实这是mysql的规范,你不能对同一张表同时进行删除查询操作,必须起个别名,“骗过” mysql的校验,哈哈

解决办法:

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

猜你喜欢

转载自blog.csdn.net/yangning_/article/details/114547080