SELECT INTO and "Undeclared variable" error

INSERT ... SELECT

http://dev.mysql.com/doc/refman/5.1/en/insert-select.html

INSERT INTO newsletter_to_send
SELECT id_subscriber FROM subscribers 

PS: are you sure you don't need in WHERE clause?


MySQL does not support the SELECT ... INTO ... syntax.

You have to use the INSERT INTO ... SELECT .. syntax to accomplish there.

Read more here.. http://dev.mysql.com/doc/refman/5.0/en/insert-select.html


I think you can follow my given way and hopefully you will be able to fix your problem.

At first use this sql command to create a new table where you want to take backup

CREATE TABLE destination_table_name LIKE source_table_name;

After then you can use this command to copy those data

INSERT INTO destination_table_name
SELECT * FROM source_table_name;

If you already have previous data in your Destination table , Firstly you can use this command

TRUNCATE TABLE destination_table_name; 

Thanks By Md. Maruf Hossain


MySQL does not support SELECT INTO [table]. It only supports SELECT INTO [variable] and can only do this one variable at a time.

What you can do, however is use the CREATE TABLE syntax with a SELECT like so:

CREATE TABLE bar ([column list]) SELECT * FROM foo;

CREATE TABLE table_name
AS  
SELECT ...(your select)