SELECT INTO and "Undeclared variable" error

MysqlSelect Into

Mysql Problem Overview


When I try to execute following query:

SELECT id_subscriber
  INTO newsletter_to_send
  FROM subscribers 

I get an error:

> #1327 - Undeclared variable: newsletter_to_send

What is wrong with that query ?

Mysql Solutions


Solution 1 - Mysql

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?

Solution 2 - Mysql

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

Solution 3 - Mysql

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

Solution 4 - Mysql

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;

Solution 5 - Mysql

CREATE TABLE table_name
AS

SELECT ...(your select)

Solution 6 - Mysql

I tried working on this just now and this works for me:

CREATE TABLE New_Table_name
SELECT * FROM Original_Table_name;

Solution 7 - Mysql

MySQL Server doesn't support the SELECT ... INTO TABLE Sybase SQL extension. Instead, MySQL Server supports the INSERT INTO ... SELECT standard SQL syntax, which is basically the same thing. See Section 12.2.5.1, “INSERT ... SELECT Syntax”.

Ref:- this

Solution 8 - Mysql

mysql don't support SELECT ... INTO ... syntax,

if it's a new table, use CREATE TABLE ... SELECT ... syntax.

example:

CREATE TABLE artists_and_works
  SELECT artist.name, COUNT(work.artist_id) AS number_of_works
  FROM artist LEFT JOIN work ON artist.id = work.artist_id
  GROUP BY artist.id;

read more here create-table-select

Solution 9 - Mysql

If you want to create a new table as a Duplicate table.

CREATE TABLE WorkerClone LIKE Worker;	//only structure will show no data
CREATE TABLE WorkerClone Select * from  Worker;	//DUPLICATE table created with all details.

Solution 10 - Mysql

You can do this without CREATE TABLE + INSERT INTO + DROP TABLE Like this:

SELECT @newsletter_to_send := id_subscriber
FROM subscribers WHERE your_condition = your_value;

Solution 11 - Mysql

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:

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionhszView Question on Stackoverflow
Solution 1 - MysqlzerkmsView Answer on Stackoverflow
Solution 2 - MysqlRaj MoreView Answer on Stackoverflow
Solution 3 - MysqlMd. Maruf HossainView Answer on Stackoverflow
Solution 4 - MysqlamphetamachineView Answer on Stackoverflow
Solution 5 - MysqlFabiano SharkView Answer on Stackoverflow
Solution 6 - Mysqluser2365440View Answer on Stackoverflow
Solution 7 - MysqlSalilView Answer on Stackoverflow
Solution 8 - MysqlDl LiView Answer on Stackoverflow
Solution 9 - MysqlPrakash SinghView Answer on Stackoverflow
Solution 10 - MysqlMTKView Answer on Stackoverflow
Solution 11 - MysqlsidduView Answer on Stackoverflow