How to Transfer Data from One Table to Another in MySQL
When faced with the task of transferring data from one table to another, the INSERT statement can come in handy. In your specific case, you encountered an error with your query:
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')
The error message indicates that the number of columns in the INSERT statement doesn't match the number of values provided. The issue here is that you're attempting to insert the results of a subquery as a single value, which is not possible.
The Solution: INSERT...SELECT Syntax
To address this, you should use the INSERT...SELECT syntax, which allows you to directly insert data from one table into another. Here's the corrected query:
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
In this revised query:
With this correction, your query should successfully insert the data from tbl_magazine_subscription into mt_magazine_subscription with the specified status value.
The above is the detailed content of How to Fix \'Column Count Mismatch\' Error When Transferring Data Between MySQL Tables?. For more information, please follow other related articles on the PHP Chinese website!