Home > Net >  MYSQL stored procedure arithmetic addition amount every number
MYSQL stored procedure arithmetic addition amount every number

Time:11-30

already have the code in php, but I want implement it to mysql stored procedure?

this my php code:

$array = [];
$amount = 0;
$add = 20;

for($i = 1; $i <= 10; $i  )
{
    $amount  = $add;
    $array[] = $amount;
}

print_r($array);
result : [20,40,60,80,100,120,140,160,180,200];

My Stored Procedure:

DELIMITER $$

CREATE PROCEDURE RepeatDemo()
BEGIN
    DECLARE counter INT DEFAULT 1;
    DECLARE result VARCHAR(327) DEFAULT '';
    
    REPEAT
        SET result = CONCAT(result,counter,',');
        SET counter = counter   20;
    UNTIL counter >= 10
    END REPEAT;
    
    -- display result
    SELECT result;
END$$

DELIMITER ;

but that didn't work

CodePudding user response:

CREATE PROCEDURE RepeatDemo()
BEGIN
    DECLARE counter INT DEFAULT 1;
    DECLARE result TEXT;
    
    REPEAT
        SET result = CONCAT_WS(',', result,counter * 20);
        SET counter = counter   1;
    UNTIL counter >= 10
    END REPEAT;
    
    -- display result
    SELECT result;
END

https://dbfiddle.uk/XJfYfXiV

  • Related