MySQL 程序错误 "Truncated incorrect DOUBLE value"
MySQL error "Truncated incorrect DOUBLE value" in procedure
我的程序有问题。我尝试从 sales table 中获取值并使用它们进行查询。
过程如下所示:
DROP PROCEDURE IF EXISTS turnover;
DELIMITER $$
CREATE PROCEDURE turnover()
BEGIN
DECLARE col INT;
DECLARE q TEXT;
DECLARE i INT DEFAULT 0;
DECLARE m TEXT;
SET col = (SELECT count(DISTINCT article) FROM sales);
SET q = "SELECT article, ";
WHILE i < co DO
SET m = (SELECT DISTINCT month FROM sales LIMIT 1 OFFSET i);
SET q = q + "SUM(IF(month=" + m + ",value,NULL)) AS " + m;
IF i < (col - 1) THEN
SET q = q + ", ";
END IF;
SET i = i + 1;
END WHILE;
SET q = q + " FROM sales GROUP BY article";
EXECUTE q;
END$$
DELIMITER ;
CALL turnover();
我收到错误:
错误代码:1292。截断不正确的 DOUBLE 值:',value,NULL)) AS '
我怎样才能让它发挥作用?
谢谢。
当 SELECT DISTINCT month FROM sales
没有 return 任何东西时就会发生这种情况。在下一行,查询片段生成为 SUM(IF(month=,value,NULL)) AS
,当然,那里有一个错误(也许 MySQL 不会产生正确的错误消息,但这就是错误所在) .
错误的原因是 WHILE
行将 i
与未知变量 co
进行比较。它可能应该是:
WHILE i < col DO
但这并没有解决问题,因为 col
是 article
的不同值的数量,并且您在 [=19 的不同值上从 1 迭代到 col
=].很可能它们的数量不同,如果文章数量更多,那么错误将再次发生。
col
问题已在下面修复或假定。
CREATE SCHEMA safe_Tuesday_01; -- safe sandbox
USE safe_Tuesday_01; -- DO the work in this db to test it
-- a fake table, we need something
create table sales
( article varchar (100) not null,
month int not null
);
第1步,找出字符串的样子:
DROP PROCEDURE IF EXISTS turnover;
DELIMITER $$
CREATE PROCEDURE turnover()
BEGIN
DECLARE col INT;
DECLARE q TEXT;
DECLARE i INT DEFAULT 0;
DECLARE m TEXT;
SET col = (SELECT count(DISTINCT article) FROM sales);
SET q = "SELECT article, ";
WHILE i < col DO
SET m = (SELECT DISTINCT month FROM sales LIMIT 1 OFFSET i);
SET q = CONCAT(q,"SUM(IF(month=" + m + ",value,NULL)) AS ", m);
IF i < (col - 1) THEN
SET q = q + ", ";
END IF;
SET i = i + 1;
END WHILE;
SET q = CONCAT(q," FROM sales GROUP BY article");
select q;
-- EXECUTE q; -- No no no this is wrong anyway
END$$
DELIMITER ;
CALL turnover();
SELECT article, FROM sales GROUP BY article
上面SELECT
看起来没那么热。在第 1 步中重复修复您的逻辑以修复该字符串。
第 2 步,当你修改上面的代码时,把下面的代码放进去。请注意,目前它还不是固定的。同样,上面那样做。
但在下文中,使用正确的 PREPARED STATEMENT
而你不是。
DROP PROCEDURE IF EXISTS turnover;
DELIMITER $$
CREATE PROCEDURE turnover()
BEGIN
DECLARE col INT;
DECLARE q TEXT;
DECLARE i INT DEFAULT 0;
DECLARE m TEXT;
SET col = (SELECT count(DISTINCT article) FROM sales);
SET q = "SELECT article, ";
WHILE i < col DO
SET m = (SELECT DISTINCT month FROM sales LIMIT 1 OFFSET i);
SET q = CONCAT(q,"SUM(IF(month=" + m + ",value,NULL)) AS ", m);
IF i < (col - 1) THEN
SET q = q + ", ";
END IF;
SET i = i + 1;
END WHILE;
SET q = CONCAT(q," FROM sales GROUP BY article");
-- select q;
SET @theSQL=q;
PREPARE stmt1 FROM @theSQL;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
END$$
DELIMITER ;
完成后,
DROP SCHEMA safe_Tuesday_01; -- clean up, poof, sandbox is gone
CONCAT
is your friend. You missed this step. It is important that the PREPARE
适用于用户变量(带有 @
符号)而不是本地变量(来自 DECLARE
),否则它会爆炸。所以我用 @theSQL
修复了上面的问题
再次参见 MySQL 手册页 PREPARE Syntax。正确设置字符串很重要。这就是第 1 步的重点。然后您才能继续第 2 步并使用它。
我的程序有问题。我尝试从 sales table 中获取值并使用它们进行查询。 过程如下所示:
DROP PROCEDURE IF EXISTS turnover;
DELIMITER $$
CREATE PROCEDURE turnover()
BEGIN
DECLARE col INT;
DECLARE q TEXT;
DECLARE i INT DEFAULT 0;
DECLARE m TEXT;
SET col = (SELECT count(DISTINCT article) FROM sales);
SET q = "SELECT article, ";
WHILE i < co DO
SET m = (SELECT DISTINCT month FROM sales LIMIT 1 OFFSET i);
SET q = q + "SUM(IF(month=" + m + ",value,NULL)) AS " + m;
IF i < (col - 1) THEN
SET q = q + ", ";
END IF;
SET i = i + 1;
END WHILE;
SET q = q + " FROM sales GROUP BY article";
EXECUTE q;
END$$
DELIMITER ;
CALL turnover();
我收到错误:
错误代码:1292。截断不正确的 DOUBLE 值:',value,NULL)) AS '
我怎样才能让它发挥作用?
谢谢。
当 SELECT DISTINCT month FROM sales
没有 return 任何东西时就会发生这种情况。在下一行,查询片段生成为 SUM(IF(month=,value,NULL)) AS
,当然,那里有一个错误(也许 MySQL 不会产生正确的错误消息,但这就是错误所在) .
错误的原因是 WHILE
行将 i
与未知变量 co
进行比较。它可能应该是:
WHILE i < col DO
但这并没有解决问题,因为 col
是 article
的不同值的数量,并且您在 [=19 的不同值上从 1 迭代到 col
=].很可能它们的数量不同,如果文章数量更多,那么错误将再次发生。
col
问题已在下面修复或假定。
CREATE SCHEMA safe_Tuesday_01; -- safe sandbox
USE safe_Tuesday_01; -- DO the work in this db to test it
-- a fake table, we need something
create table sales
( article varchar (100) not null,
month int not null
);
第1步,找出字符串的样子:
DROP PROCEDURE IF EXISTS turnover;
DELIMITER $$
CREATE PROCEDURE turnover()
BEGIN
DECLARE col INT;
DECLARE q TEXT;
DECLARE i INT DEFAULT 0;
DECLARE m TEXT;
SET col = (SELECT count(DISTINCT article) FROM sales);
SET q = "SELECT article, ";
WHILE i < col DO
SET m = (SELECT DISTINCT month FROM sales LIMIT 1 OFFSET i);
SET q = CONCAT(q,"SUM(IF(month=" + m + ",value,NULL)) AS ", m);
IF i < (col - 1) THEN
SET q = q + ", ";
END IF;
SET i = i + 1;
END WHILE;
SET q = CONCAT(q," FROM sales GROUP BY article");
select q;
-- EXECUTE q; -- No no no this is wrong anyway
END$$
DELIMITER ;
CALL turnover();
SELECT article, FROM sales GROUP BY article
上面SELECT
看起来没那么热。在第 1 步中重复修复您的逻辑以修复该字符串。
第 2 步,当你修改上面的代码时,把下面的代码放进去。请注意,目前它还不是固定的。同样,上面那样做。
但在下文中,使用正确的 PREPARED STATEMENT
而你不是。
DROP PROCEDURE IF EXISTS turnover;
DELIMITER $$
CREATE PROCEDURE turnover()
BEGIN
DECLARE col INT;
DECLARE q TEXT;
DECLARE i INT DEFAULT 0;
DECLARE m TEXT;
SET col = (SELECT count(DISTINCT article) FROM sales);
SET q = "SELECT article, ";
WHILE i < col DO
SET m = (SELECT DISTINCT month FROM sales LIMIT 1 OFFSET i);
SET q = CONCAT(q,"SUM(IF(month=" + m + ",value,NULL)) AS ", m);
IF i < (col - 1) THEN
SET q = q + ", ";
END IF;
SET i = i + 1;
END WHILE;
SET q = CONCAT(q," FROM sales GROUP BY article");
-- select q;
SET @theSQL=q;
PREPARE stmt1 FROM @theSQL;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
END$$
DELIMITER ;
完成后,
DROP SCHEMA safe_Tuesday_01; -- clean up, poof, sandbox is gone
CONCAT
is your friend. You missed this step. It is important that the PREPARE
适用于用户变量(带有 @
符号)而不是本地变量(来自 DECLARE
),否则它会爆炸。所以我用 @theSQL
再次参见 MySQL 手册页 PREPARE Syntax。正确设置字符串很重要。这就是第 1 步的重点。然后您才能继续第 2 步并使用它。