我尽力想为这个问题起一个更好的标题,但那是我能做出来的全部。好吧,我将尝试通过举例来解释它。假设我有。 Set x = 100200300; Set y = 10; BNow I want to add y into all first 3 numbers which is "100" then add it into "200" and so on. 我想知道我是否可以在循环中使用 "select left "函数来实现?不知道如何应用它,因此我在这里寻求帮助。请帮助我。祝福
DELIMITER $$
CREATE PROCEDURE proc(
)
BEGIN
DECLARE counter INT DEFAULT 1;
set @x = '100200300400';
set @y = 10;
WHILE counter <= @x DO
select concat(substring(@x,counter,3) + @y,substring(@x,(counter +3),3) + @y) x;
END WHILE;
END$$
DELIMITER ;
可能是
SET @X = 100200300;
SET @Y = 10;
select concat(substring(@x,1,3) + @y,substring(@x,4,3) + @y,substring(@x,7,3) + @y) x;
+-----------+
| x |
+-----------+
| 110210310 |
+-----------+
1 row in set (0.001 sec)
如果x是未知长度,DROP PROCEDURE IF EXISTS P; DELIMITER $$ CREATE PROCEDURE p(
)
BEGIN
DECLARE counter INT DEFAULT 1;
set @x = '100200300400';
set @y = 10;
SET @OUT = '';
L:WHILE counter <= @x / 3 DO
#SELECT COUNTER;
IF @x is null or COUNTER > 10 THEN LEAVE L; END IF;
SET @OUT = CONCAT(@OUT,substring(@x,counter,3) + @y);
SET @X = REPLACE(@X,@out,'');
#select concat(substring(@x,counter,3) + @y,substring(@x,(counter +3),3) + @y) x;
SET COUNTER = COUNTER + 3;
END WHILE;
select @out;
END $$
DELIMITER ;
CALL P();
DELIMITER ;