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

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

    Make a temporary table (tempP) from a subquery

    UPDATE pers P 
    SET P.gehalt = P.gehalt * 1.05 
    WHERE P.persID IN (
        SELECT tempP.tempId
        FROM (
            SELECT persID as tempId
            FROM pers P
            WHERE
                P.chefID IS NOT NULL OR gehalt < 
                    (SELECT (
                        SELECT MAX(gehalt * 1.05) 
                        FROM pers MA 
                        WHERE MA.chefID = MA.chefID) 
                        AS _pers
                    )
        ) AS tempP
    )
    

    I've introduced a separate name (alias) and give a new name to 'persID' column for temporary table

提交回复
热议问题