You can't specify target table for update in FROM clause

前端 未结 11 1372
野趣味
野趣味 2020-11-21 22:46

I have a simple mysql table:

CREATE TABLE IF NOT EXISTS `pers` (
  `persID` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(35) NOT NULL,
  `gehalt` int(11         


        
11条回答
  •  面向向阳花
    2020-11-21 23:00

    The Approach posted by BlueRaja is slow I modified it as I was using to delete duplicates from the table. In case it helps anyone with large tables Original Query

    delete from table where id not in (select min(id) from table group by field 2)
    

    This is taking more time:

    DELETE FROM table where ID NOT IN(
      SELECT MIN(t.Id) from (select Id,field2 from table) AS t GROUP BY field2)
    

    Faster Solution

    DELETE FROM table where ID NOT IN(
       SELECT x.Id from (SELECT MIN(Id) as Id from table GROUP BY field2) AS t)
    

提交回复
热议问题