Select from same table as an Insert or Update

前端 未结 5 1869
走了就别回头了
走了就别回头了 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:33

    try:

    insert into aTable select max(a)^2, 'name' from aTable;
    

    or

    insert into aTable select max(a)^2, 'name' from aTable group by B;
    

    If you need a join, you can do this:

    insert into aTable select max(a)^2, 'name' from aTable, bTable;
    

    My "Server version" is "5.0.51b-community-nt MySQL Community Edition (GPL)"

提交回复
热议问题