83

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_subscription_id, 
      subscription_name, 
      magazine_id, 
      status ) 
  VALUES ( 
      (SELECT magazine_subscription_id, 
              subscription_name, 
              magazine_id 
       FROM tbl_magazine_subscription 
       ORDER BY magazine_subscription_id ASC), '1')

but i got an error that

  #1136 - Column count doesn't match value count at row 1

please help me.

0

12 Answers 12

149

You can use INSERT...SELECT syntax. Note that you can quote '1' directly in the SELECT part.

INSERT INTO mt_magazine_subscription ( 
      magazine_subscription_id, 
      subscription_name, 
      magazine_id, 
      status ) 
SELECT magazine_subscription_id, 
       subscription_name, 
       magazine_id, 
       '1'
FROM tbl_magazine_subscription
ORDER BY magazine_subscription_id ASC 
3
  • Its helpful and i found it quite fast in adding 30k + rows compare to other. Thank you.
    – Jarnail S
    Aug 2, 2018 at 7:19
  • what if I want to edit the data before inserting in the new one? Aug 27, 2018 at 14:53
  • @JohnDemetriou You can transform the data by doing something to the values in the SELECT part of the statement. For example, subscription_name + '_old'. If you have something more complex in mind, why not ask a new SO question? Aug 27, 2018 at 14:58
33

If you want insert all data from one table to another table there is a very simply sql

INSERT INTO destinationTable  (SELECT * FROM sourceDbName.SourceTableName);
5
  • 5
    I think this is the best answer
    – BARIS KURT
    Dec 25, 2016 at 22:54
  • Amazing. But I have a question: why this work? What this select returns to insert into ?
    – Marcelo
    Jul 15, 2017 at 17:23
  • @Gustav Bertram number of columns you are selecting from the source table and number of columns in destination table is not same. In insert part of SQL you are using four columns and in selection part you are selecting only three columns .
    – Ravi
    Jun 23, 2018 at 6:07
  • 1
    Its not helpful when you want to insert specific columns values Oct 30, 2018 at 5:57
  • @KiranJoshi in that case mention column names in your select query.
    – Ravi
    Dec 9, 2018 at 17:54
5

It wont work like this.

When you try to insert the row using a query all values should be there in query.

With the above problem you want to insert magazine_subscription_id, subscription_name, magazine_id, status in select query you have magazine_subscription_id, subscription_name, magazine_id, status 1 it is not possible.

If you want to insert either you need to insert using query of direct values

5

Actually the mysql query for copy data from one table to another is

Insert into table2_name (column_names) select column_name from table1

where, the values copied from table1 to table2

5

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 php2);

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.

2
INSERT INTO mt_magazine_subscription ( 
      magazine_subscription_id, 
      subscription_name, 
      magazine_id, 
      status ) 
VALUES ( 
      (SELECT magazine_subscription_id, 
              subscription_name, 
              magazine_id,'1' as status
       FROM tbl_magazine_subscription 
       ORDER BY magazine_subscription_id ASC));
1

Insert data from one table to other with condition in MySQL and same will work in SQL Server as well. Only non existing data will get updated. Both table have same structure so column need not to pass.

insert into table_A
select * from table_A_copy 
where not exists
(
select * from table_A where table_A_copy.clm_a=table_A.clm_a and table_A_copy.clm_b=table_A.clm_b and table_A_copy.clm_c=table_A.clm_c
);
0

Try this. Your doing in wrong way.

    INSERT INTO mt_magazine_subscription( 
    magazine_subscription_id, 
    subscription_name, 
    magazine_id, status) VALUES ( 
         (SELECT magazine_subscription_id, subscription_name, 
                 magazine_id,1 as status FROM tbl_magazine_subscription 
                 ORDER BY magazine_subscription_id ASC)
    )
1
0
  INSERT INTO mt_magazine_subscription ( 
      magazine_subscription_id, 
      subscription_name, 
      magazine_id, 
      status ) 
  VALUES ( 
      (SELECT magazine_subscription_id, 
              subscription_name, 
              magazine_id,'1' as status
       FROM tbl_magazine_subscription 
       ORDER BY magazine_subscription_id ASC))
1
0

Try to use this

INSERT INTO mt_magazine_subscription ( 
      magazine_subscription_id, 
      subscription_name, 
      magazine_id, 
      status ) 
SELECT magazine_subscription_id, 
       subscription_name, 
       magazine_id, 
       '1'
FROM tbl_magazine_subscription
ORDER BY magazine_subscription_id ;

Use the hard coded value in select clause

1
  • Please add further details to expand on your answer, such as working code or documentation citations.
    – Community Bot
    Sep 10, 2021 at 10:05
-1
INSERT INTO destination_table ( 
      Field_1, 
      Field_2, 
      Field_3) 
SELECT Field_1, 
      Field_2, 
      Field_3 
      FROM source_table;

BUT this is a BAD MYSQL

Do this instead:

  1. drop the destination table: DROP DESTINATION_TABLE;
  2. CREATE TABLE DESTINATION_TABLE AS (SELECT * FROM SOURCE_TABLE);
3
  • 3
    what do you mean by BAD MYSQL?? and What if we already have some data in DESTINATION_TABLE?? Jun 26, 2015 at 6:02
  • 2
    This does not answer the question about INSERT/SELECT with different number of columns in both tables...
    – Marki555
    Jun 26, 2015 at 9:53
  • 1
    The person in the original question may have had data in the destination table. Dropping a table does not sound like a good all purpose answer due to that possibility.
    – chrisfs
    Sep 2, 2015 at 19:31
-2
INSERT INTO mt_magazine_subscription SELECT *
       FROM tbl_magazine_subscription 
       ORDER BY magazine_subscription_id ASC

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.