[英]mysql Creating a temporary table not working…undeclared variable error…whats wrong with my code?
When I run the code below inside phpmyadmin I get an error "Undefined Variable tmpportfolio" 当我在phpmyadmin中运行以下代码时,出现错误“未定义的变量tmpportfolio”
what is the problem? 问题是什么?
SQL: SQL:
CREATE TEMPORARY TABLE tmpportfolio (p_name VARCHAR(255),portfolio_id INT UNSIGNED NOT NULL);
SELECT p_name, portfolio_id INTO tmpportfolio
FROM portfolio
WHERE parent_portfolio_id IS NULL
AND portfolio_id NOT IN
(
SELECT x.parent_portfolio_id
FROM portfolio x
WHERE x.parent_portfolio_id IS NOT NULL
)
AND account_id = 1
SELECT * FROM tmpportfolio;
SELECT field INTO variable
loads the value of the field into a variable. SELECT field INTO variable
将SELECT field INTO variable
的值加载到变量中。 This is why it's complaining about the variable not being defined. 这就是为什么它抱怨未定义变量。
What you need to do is use an INSERT INTO table (colummn) SELECT field
statement instead. 您需要做的是改为使用INSERT INTO table (colummn) SELECT field
语句。 For instance: 例如:
INSERT INTO tmpportfolio (p_name, portfolio_id)
SELECT p_name, portfolio_id
FROM portfolio
WHERE parent_portfolio_id IS NULL
AND portfolio_id NOT IN
(
SELECT x.parent_portfolio_id
FROM portfolio x
WHERE x.parent_portfolio_id IS NOT NULL
)
AND account_id = 1
You cannot use INTO
to insert records into a table in MySql (in SQL Server, you do use INTO
that way). 您不能使用INTO
将记录插入到MySql中的表中(在SQL Server中,您确实使用INTO
)。 In MySql, INTO
is used to put a value into a variable. 在MySql中, INTO
用于将值放入变量。
By doing the SELECT fields INTO tmpportfolio
statement, MySql thinks that tmpportfolio
is now a variable and it cannot find the variable in the SELECT * ..
. 通过执行SELECT fields INTO tmpportfolio
语句,MySql认为tmpportfolio
现在是一个变量,并且无法在SELECT * ..
找到该变量。 The fix is to change the insert-records sql to something like: 解决方法是将insert-records sql更改为以下内容:
INSERT INTO tmpportfolio(p_name,portfolio_id) SELECT ....;
Just join first two SQL statement in one CREATE TABLE ... SELECT statment: 只需在一个CREATE TABLE ... SELECT语句中加入前两个SQL语句:
CREATE TABLE tmpportfolio SELECT p_name, portfolio_id FROM portfolio
WHERE parent_portfolio_id IS NULL
AND portfolio_id NOT IN
(
SELECT x.parent_portfolio_id
FROM portfolio x
WHERE x.parent_portfolio_id IS NOT NULL
)
AND account_id = 1
SELECT * FROM tmpportfolio;
我无法单独代表mysql,但是通常在创建表后将其插入表中就不会使用selct来创建表(并且因为表已经存在,所以不能使用selct)。
The confusion seems to arise from W3Schools explaining how to do "select into table" without mentioning it's specific to some (non-Mysql) implementations. W3Schools在解释如何执行“选择到表中”而未提及特定于某些(非Mysql)实现时似乎产生了困惑。 A page on dev.mysql specifically denies this possibility on Mysql, by saying: 在dev.mysql的页面上 ,通过说:
MySQL Server doesn't support the SELECT ... INTO TABLE Sybase SQL extension. MySQL服务器不支持SELECT ... INTO TABLE Sybase SQL扩展。 Instead, MySQL Server supports the INSERT INTO ... SELECT standard SQL syntax, which is basically the same thing. 相反,MySQL Server支持INSERT INTO ... SELECT标准SQL语法,这基本上是相同的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.