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

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

    MySQL doesn't allow selecting from a table and update in the same table at the same time. But there is always a workaround :)

    This doesn't work >>>>

    UPDATE table1 SET col1 = (SELECT MAX(col1) from table1) WHERE col1 IS NULL;
    

    But this works >>>>

    UPDATE table1 SET col1 = (SELECT MAX(col1) FROM (SELECT * FROM table1) AS table1_new) WHERE col1 IS NULL;
    

提交回复
热议问题