给数字字符串的每 3 个数字添加一个常量
Adding a constant to each 3 numbers of a number string
我尽力为问题取一个更好的标题,但这就是我所能弥补的。好吧,我将尝试通过举例来解释它。假设我有: Set x = 100200300;
设置 y = 10;乙
现在我想将 y 添加到所有前 3 个数字中,即“100”,然后将其添加到“200”中,依此类推。我想知道我是否可以在循环中使用“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 的长度未知
如果存在则删除过程 P;
分隔符 $$
创建过程 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 ;
我尽力为问题取一个更好的标题,但这就是我所能弥补的。好吧,我将尝试通过举例来解释它。假设我有: Set x = 100200300; 设置 y = 10;乙 现在我想将 y 添加到所有前 3 个数字中,即“100”,然后将其添加到“200”中,依此类推。我想知道我是否可以在循环中使用“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 的长度未知 如果存在则删除过程 P; 分隔符 $$ 创建过程 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 ;