MySQL SELECT INTO 和“未声明的变量”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2949653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SELECT INTO and "Undeclared variable" error
提问by hsz
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
#1327 - 未声明的变量:newsletter_to_send
What is wrong with that query ?
该查询有什么问题?
回答by zerkms
INSERT ... SELECT
http://dev.mysql.com/doc/refman/5.1/en/insert-select.html
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?
PS:你确定你不需要 inWHERE
条款?
回答by Raj More
MySQL does not support the SELECT ... INTO ...
syntax.
MySQL 不支持该SELECT ... INTO ...
语法。
You have to use the INSERT INTO ... SELECT ..
syntax to accomplish there.
你必须使用INSERT INTO ... SELECT ..
语法来完成那里。
Read more here.. http://dev.mysql.com/doc/refman/5.0/en/insert-select.html
在这里阅读更多.. http://dev.mysql.com/doc/refman/5.0/en/insert-select.html
回答by Md. Maruf Hossain
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
首先使用此 sql 命令创建一个要备份的新表
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
感谢 Md. Maruf Hossain
回答by Fabiano Shark
CREATE TABLE table_name
AS
SELECT ...(your select)
回答by amphetamachine
MySQL does not support SELECT INTO [table]
. It only supports SELECT INTO [variable]
and can only do this one variable at a time.
MySQL 不支持SELECT INTO [table]
. 它一次只支持SELECT INTO [variable]
并且只能执行一个变量。
What you cando, however is use the CREATE TABLE
syntax with a SELECT
like so:
但是,您可以做的是使用类似这样的CREATE TABLE
语法SELECT
:
CREATE TABLE bar ([column list]) SELECT * FROM foo;
回答by user2365440
I tried working on this just now and this works for me:
我刚刚尝试解决这个问题,这对我有用:
CREATE TABLE New_Table_name
SELECT * FROM Original_Table_name;
回答by Salil
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”.
MySQL Server 不支持 SELECT ... INTO TABLE Sybase SQL 扩展。相反,MySQL Server 支持 INSERT INTO ... SELECT 标准 SQL 语法,这基本上是相同的。请参阅第 12.2.5.1 节,“插入 ... SELECT 语法”。
Ref:- this
参考:-这个
回答by Dl Li
mysql don't support SELECT ... INTO ... syntax,
mysql 不支持 SELECT ... INTO ... 语法,
if it's a new table, use CREATE TABLE ... SELECT ... syntax.
如果是新表,请使用 CREATE TABLE ... SELECT ... 语法。
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
在此处阅读更多信息create-table-select
回答by Prakash Singh
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.
回答by siddu
MySQL does not support SELECT INTO [table]
. It only supports SELECT INTO [variable]
and can only do this one variable at a time.
MySQL 不支持SELECT INTO [table]
. 它一次只支持SELECT INTO [variable]
并且只能执行一个变量。
What you can do, however is use the CREATE TABLE
syntax with a SELECT
like so:
但是,您可以做的是使用类似这样的CREATE TABLE
语法SELECT
: