user15293852
user15293852

Reputation:

MYSQL stored procedure arithmetic addition amount every number

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

Upvotes: 0

Views: 48

Answers (1)

Akina
Akina

Reputation: 42728

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

Upvotes: 1

Related Questions