Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

insert data from one table to another in mysql

Tags:

mysql

insert

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.

like image 547
deepu sankar Avatar asked Feb 16 '13 07:02

deepu sankar


People also ask

How can I insert data from one table to another table in MySQL?

The SQL INSERT INTO SELECT Statement The INSERT INTO SELECT statement copies data from one table and inserts it into another table. The INSERT INTO SELECT statement requires that the data types in source and target tables match. Note: The existing records in the target table are unaffected.


1 Answers

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  
like image 97
Gustav Bertram Avatar answered Oct 14 '22 15:10

Gustav Bertram