Select from same table as an Insert or Update

前端 未结 5 1761
走了就别回头了
走了就别回头了 2021-01-17 17:48

Clearly the following is incorrect.

INSERT INTO `aTable` (`A`,`B`) VALUES((SELECT MAX(`A`) FROM `aTable`)*2),\'name\');

I get the value:

5条回答
  •  滥情空心
    2021-01-17 18:28

    I take it that INSERT ... SELECT isn't working? I see this in the documentation for it:

    The target table of the INSERT statement may appear in the FROM clause of the SELECT part of the query. (This was not possible in some older versions of MySQL.) In this case, MySQL creates a temporary table to hold the rows from the SELECT and then inserts those rows into the target table.

    Out of curiosity, which version of MySQL are you using?

提交回复
热议问题