Insert into … values ( SELECT … FROM … )

前端 未结 26 2501
我在风中等你
我在风中等你 2020-11-21 05:40

I am trying to INSERT INTO a table using the input from another table. Although this is entirely feasible for many database engines, I always seem to struggle t

26条回答
  •  鱼传尺愫
    2020-11-21 05:57

    If you go the INSERT VALUES route to insert multiple rows, make sure to delimit the VALUES into sets using parentheses, so:

    INSERT INTO `receiving_table`
      (id,
      first_name,
      last_name)
    VALUES 
      (1002,'Charles','Babbage'),
      (1003,'George', 'Boole'),
      (1001,'Donald','Chamberlin'),
      (1004,'Alan','Turing'),
      (1005,'My','Widenius');
    

    Otherwise MySQL objects that "Column count doesn't match value count at row 1", and you end up writing a trivial post when you finally figure out what to do about it.

提交回复
热议问题