我正在将一些查询滚动到一个存储过程中,但我遇到了#1327 - Undeclared variable
错误……但奇怪的是它声称未声明的变量实际上是一个表名。
解决问题
因此,我提取了它崩溃的部分过程,并尝试通过 PHPMyAdmin 直接在数据库上将其作为普通 SQL 查询运行......同样的事情。经过多次修补,它似乎是我加入另一张桌子的地方。
如果我在单个表上运行查询,那很好,如下所示:
SET @i_channel_id = 3;
SET @i_product_id = 90;
SELECT
`product_status_to_channel`.`status_code` INTO @s_status_code
FROM `product_status_to_channel`
WHERE `product_status_to_channel`.`channel_id` = @i_channel_id
AND `product_status_to_channel`.`product_id` = @i_product_id
ORDER BY IF(`product_status_to_channel`.`date` IS NULL, 1, 0) ASC,
`product_status_to_channel`.`date` DESC
LIMIT 0, 1;
SELECT @s_status_code AS status_code;
在 PHPMyAdmin中
输出'LIVE'status_code
- 这很好。
但是,当我尝试JOIN
在消息表中查找关联的状态消息时,我收到错误消息:#1327 - Undeclared variable: product_status_to_channel_lang
... 但是product_status_to_channel_lang
是表吗?!
SET @i_channel_id = 3;
SET @i_language_id = 3;
SET @i_product_id = 90;
SELECT
`product_status_to_channel`.`status_code` INTO @s_status_code,
`product_status_to_channel_lang`.`string` INTO @s_status_message
FROM `product_status_to_channel`
LEFT JOIN `product_status_to_channel_lang`
ON `product_status_to_channel`.`product_status_to_channel_id` = `product_status_to_channel_lang`.`product_status_to_channel_id`
AND `product_status_to_channel_lang`.`language_id` = @i_language_id
WHERE `product_status_to_channel`.`channel_id` = @i_channel_id
AND `product_status_to_channel`.`product_id` = @i_product_id
ORDER BY IF(`product_status_to_channel`.`date` IS NULL, 1, 0) ASC, `product_status_to_channel`.`date` DESC
LIMIT 0, 1;
SELECT @s_status_code AS status_code, @s_status_message AS status_message;
它是否试图评估product_status_to_channel_lang.product_status_to_channel_id
为 JOIN 上的变量?
LEFT JOIN `product_status_to_channel_lang`
ON `product_status_to_channel`.`product_status_to_channel_id` = `product_status_to_channel_lang`.`product_status_to_channel_id`
我假设我忽略了一些明显的东西?
我在这两个上都试过了:
- 使用 MySQL 5.5.27 运行 xampp 的 Win7 机器 - MySQL 社区服务器 (GPL)
- 一个运行 MySQL 5.1.73-1-log 的 Debian 机器 - (Debian)