insert data from one table to another in mysql

后端 未结 10 533
深忆病人
深忆病人 2020-11-29 18:39

i want to read all data from one table and insert some data in to another table. my query is

  INSERT INTO mt_magazine_subscription ( 
      magazine_subscr         


        
10条回答
  •  温柔的废话
    2020-11-29 19:31

    If there is a primary key like "id" you have to exclude it for example my php table has: id, col2,col3,col4 columns. id is primary key so if I run this code:

    INSERT INTO php  (SELECT * FROM php);
    

    I probably get this error:

    #1062 - Duplicate entry '1' for key 'PRIMARY'

    So here is the solution, I excluded "id" key:

    INSERT INTO php ( col2,col3,col4)  (SELECT col2,col3,col4 FROM php2);
    

    So my new php table has all php2 table rows anymore.

提交回复
热议问题