php—concat过程中的字符串值

kupeojn6  于 2021-06-17  发布在  Mysql
关注(0)|答案(1)|浏览(325)

**结案。**此问题不可复制或由打字错误引起。它目前不接受答案。
**想改进这个问题吗?**更新问题,使其成为堆栈溢出的主题。

两年前关门了。
改进这个问题
我知道我的问题听起来很傻,但我不能运行我的代码,因为这个问题,我所有的过程都是正确的,我唯一的一个问题是我不能使用包含以下字符串的语法:
设置@query=concat(@query,'where i.active=t');
我试着在撇号和之间用+,但是在所有情况下都不起作用。。。
这是完整的程序

CREATE PROCEDURE stocktakess()

            BEGIN
              DECLARE wid INT;
              DECLARE wname VARCHAR(20);
              DECLARE query TEXT DEFAULT '';
              DECLARE finished INT DEFAULT 0;
              DECLARE whouse_cursor CURSOR FOR SELECT Id, name FROM warehouse WHERE Id IN (1,2,3,5,8);
              DECLARE CONTINUE HANDLER FOR NOT FOUND SET finished = 1;
              OPEN whouse_cursor;
              SET @query = 'SELECT i.code,i.code2,i.description';
            get_whouse: LOOP
              FETCH whouse_cursor INTO wid, wname;
              IF finished = 1 THEN 
                LEAVE get_whouse;
              END IF;
              SET @query = CONCAT(@query, ', SUM(CASE WHEN m.warehouseID=', wid, ' THEN COALESCE(m.qtyin, 0) - COALESCE(m.qtyout, 0) ELSE 0 END) AS `', wname, '`');
              END LOOP get_whouse;
              SET @query = CONCAT(@query, ' FROM items i LEFT JOIN itemmovement m ON m.itemid = i.Id');
              SET @query = CONCAT(@query, ' WHERE i.active=T');  
              SET @query = CONCAT(@query, ' GROUP BY i.Id');
              PREPARE stmt FROM @query;
              EXECUTE stmt;
              DEALLOCATE PREPARE stmt;
            END
qzwqbdag

qzwqbdag1#

CREATE PROCEDURE stocktakess()

            BEGIN
              DECLARE wid INT;
              DECLARE wname VARCHAR(20);
              DECLARE query TEXT DEFAULT '';
              DECLARE finished INT DEFAULT 0;
              DECLARE whouse_cursor CURSOR FOR SELECT Id, name FROM warehouse WHERE Id IN (1,2,3,5,8);
              DECLARE CONTINUE HANDLER FOR NOT FOUND SET finished = 1;
              OPEN whouse_cursor;
              SET @query = 'SELECT i.code,i.code2,i.description';
            get_whouse: LOOP
              FETCH whouse_cursor INTO wid, wname;
              IF finished = 1 THEN 
                LEAVE get_whouse;
              END IF;
              SET @query = CONCAT(@query, ', SUM(CASE WHEN m.warehouseID=', wid, ' THEN COALESCE(m.qtyin, 0) - COALESCE(m.qtyout, 0) ELSE 0 END) AS `', wname, '`');
              END LOOP get_whouse;
              SET @query = CONCAT(@query, ' FROM items i LEFT JOIN itemmovement m ON m.itemid = i.Id');
              SET @query = CONCAT(@query, ' WHERE i.active=\'T\'');  
              SET @query = CONCAT(@query, ' GROUP BY i.Id');
              PREPARE stmt FROM @query;
              EXECUTE stmt;
              DEALLOCATE PREPARE stmt;
            END

相关问题