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

前端 未结 11 1351
野趣味
野趣味 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:20

    You can make this in three steps:

    CREATE TABLE test2 AS
    SELECT PersId 
    FROM pers p
    WHERE (
      chefID IS NOT NULL 
      OR gehalt < (
        SELECT MAX (
          gehalt * 1.05
        )
        FROM pers MA
        WHERE MA.chefID = p.chefID
      )
    )
    

    ...

    UPDATE pers P
    SET P.gehalt = P.gehalt * 1.05
    WHERE PersId
    IN (
      SELECT PersId
      FROM test2
    )
    DROP TABLE test2;
    

    or

    UPDATE Pers P, (
      SELECT PersId
      FROM pers p
      WHERE (
       chefID IS NOT NULL 
       OR gehalt < (
         SELECT MAX (
           gehalt * 1.05
         )
         FROM pers MA
         WHERE MA.chefID = p.chefID
       )
     )
    ) t
    SET P.gehalt = P.gehalt * 1.05
    WHERE p.PersId = t.PersId
    

提交回复
热议问题