问题描述:有个数据表test,有个字段value,如下
mysql> select * from test;
+----+------------------------------------+| id | value |+----+------------------------------------+| 3 | 3aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || 4 | 4aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || 5 | 5aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || 6 | 6aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || 7 | 7aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa |如果执行如下sql语句会报错
delete from test where id=(select min(id) from test);
报的错误是You can't specify target table 'test' for update in FROM clause。
原因是不能在操作一张表的同时更新或者删除这张表的内容。
解决方法如下:(3个步骤)
create table tmp as select min(id) as col1 from test;
delete from test where id =(select col1 from tmp);
drop table tmp
这时又遇到一个问题,如何在php中一次执行多个sql语句(当然也可以分3次执行),此问题待查