delete from teacher where id not in (select max(t.id) from teacher t group by t.name)
这条语句是删除姓名重复的,运行是报错,错误如下:
You can't specify target table 'teacher' for update in FROM clause具体什么意思,怎么改才不报错,请指点下,谢谢!

解决方案 »

  1.   


    delete teacher as a from teacher as a,(select max(t.id) id from teacher t group by t.name) as b where a.id != b.id;
      

  2.   

    delete a.* from tt as a inner join  
     (select max(t.id) id from tt t group by t.name) as b 
    on a.id != b.id;
    DELETE A.* FROM TT A LEFT JOIN (select max(t.id) id from teacher t group by t.name) as b
    ON A.ID=B.ID WHERE ISNULL(B.ID)delete A.* from TT A where id not in (select max(t.id) from teacher t group by t.name) 
      

  3.   

    应该用LEFT JOIN,不能用INNER JOIN
    DELETE A.* FROM TT A LEFT JOIN (select max(t.id) id from teacher t group by t.name) as b
    ON A.ID=B.ID WHERE ISNULL(B.ID)delete A.* from TT A where id not in (select max(t.id) from teacher t group by t.name) 
      

  4.   


    Don't you think the following is the best reason for your statement?You can't specify target table 'teacher' for update in FROM clause 
      

  5.   


    TT是什么?改成teacher依然报错You can't specify target table 'teacher' for update in FROM clause,还是不清楚为什么?其他的都行!
      

  6.   

    MYSQL不支持
    delete from teacher where id not in (select max(t.id) from teacher t group by t.name) 
    这种格式,两表删除要用我与yueliangdao0608的格式
      

  7.   

    是这个问题,我试了下sqlserver中可以的,谢谢两位了!