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
是你的朋友。你錯過了這一步。 PREPARE
對於用戶變量(帶有@
符號)而不是本地變量(來自DECLARE
)是非常重要的,否則它將炸燬。所以我用@theSQL
修正了上面的問題,再次參見MySQL Manual Page PREPARE Syntax。重要的是讓你的字符串正確。這是步驟1的要點。只有這樣,才能進入步驟2並使用它。
不知道這是否是你的問題,但應該這一行「WHILE我
JanR
這很難說清楚。你爲什麼不記錄生成的語句?順便說一句,這可能甚至不需要存儲過程。 – e4c5
這個問題與我的MySQL Workbench有什麼關係? – axiac