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

前端 未结 11 1359
野趣味
野趣味 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 22:57

    MariaDB has lifted this starting from 10.3.x (both for DELETE and UPDATE):

    UPDATE - Statements With the Same Source and Target

    From MariaDB 10.3.2, UPDATE statements may have the same source and target.

    Until MariaDB 10.3.1, the following UPDATE statement would not work:

    UPDATE t1 SET c1=c1+1 WHERE c2=(SELECT MAX(c2) FROM t1);
      ERROR 1093 (HY000): Table 't1' is specified twice, 
      both as a target for 'UPDATE' and as a separate source for data
    

    From MariaDB 10.3.2, the statement executes successfully:

    UPDATE t1 SET c1=c1+1 WHERE c2=(SELECT MAX(c2) FROM t1);
    

    DELETE - Same Source and Target Table

    Until MariaDB 10.3.1, deleting from a table with the same source and target was not possible. From MariaDB 10.3.1, this is now possible. For example:

    DELETE FROM t1 WHERE c1 IN (SELECT b.c1 FROM t1 b WHERE b.c2=0);
    

    DBFiddle MariaDB 10.2 - Error

    DBFiddle MariaDB 10.3 - Success

提交回复
热议问题