58

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 ?

Clomp
  • 3,168
  • 2
  • 23
  • 36
hsz
  • 148,279
  • 62
  • 259
  • 315
  • MySQL 8.0 uses `select ... into` statements. See: https://dev.mysql.com/doc/refman/8.0/en/select-into.html – Clomp Oct 01 '22 at 22:10

11 Answers11

58
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?

zerkms
  • 249,484
  • 69
  • 436
  • 539
  • 4
    For using insert into we need to create the table first , in this case if table newsletter_to_send doesn't exist , there will be error! – Praveen Kumar Jul 28 '14 at 07:05
  • @Praveen Kumar: it's obvious that you need to have a server, with mysql installed and table created, isn't it? – zerkms Jul 28 '14 at 07:46
  • 10
    Actually , I mentioned it even more because for select ...into syntax table will be created if it doesn't exist , however with insert into it isn't so. – Praveen Kumar Jul 28 '14 at 07:55
  • @Praveen Kumar: "`select ...into` syntax" --- any reference in documentation? I'm not aware of such syntax. – zerkms Jul 28 '14 at 07:59
  • would you mind to help me with issue here please http://stackoverflow.com/questions/43987326/converting-oracle-trigger-to-mysql-trigger – sam May 15 '17 at 21:07
  • @zerkms I believe that `select ...into` is for SQL Server, not MySQL. See [https://stackoverflow.com/questions/16683758/](https://stackoverflow.com/questions/16683758/) – Paul Chris Jones Dec 19 '18 at 21:10
  • MySQL 8.0 uses `select ... into` statements. See: https://dev.mysql.com/doc/refman/8.0/en/select-into.html – Clomp Oct 01 '22 at 22:09
27

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

26

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

Raj More
  • 47,048
  • 33
  • 131
  • 198
19
CREATE TABLE table_name
AS  
SELECT ...(your select)
Fabiano Shark
  • 384
  • 2
  • 6
  • 2
    well... this is way better, especially if you don't know (or you don't want to write ) all the columns' names – salvob Nov 01 '18 at 17:09
16

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;
amphetamachine
  • 27,620
  • 12
  • 60
  • 72
  • 2
    This example is good and allows mysql to do the equivalent of SELECT ... INTO ... by auto creating a table – ejectamenta Feb 06 '15 at 13:04
  • At least for the latest version of MySQL you can assign multiple values to multiple variables in one `SELECT [values] INTO [variables]` statement. Check https://dev.mysql.com/doc/refman/5.7/en/select-into.html – El Gucs Aug 16 '17 at 09:50
7

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

CREATE TABLE New_Table_name
SELECT * FROM Original_Table_name;
El Gucs
  • 897
  • 9
  • 18
user2365440
  • 81
  • 1
  • 1
4

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

Salil
  • 46,566
  • 21
  • 122
  • 156
2

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

Dl Li
  • 36
  • 3
0

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.
Tanveer Badar
  • 5,438
  • 2
  • 27
  • 32
Prakash Singh
  • 377
  • 2
  • 4
0

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;
MTK
  • 3,300
  • 2
  • 33
  • 49
-2

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:

Nikolay K
  • 3,770
  • 3
  • 25
  • 37
siddu
  • 19