MySQL FOR Loop: Implementation Guide

In MySQL, there is no direct syntax for a for loop, but you can use loop statements and variables to achieve similar functionality. Here is an example:

DELIMITER $$
CREATE PROCEDURE for_loop_example()
BEGIN
    DECLARE i INT DEFAULT 1;
    
    WHILE i <= 10 DO
        -- 这里可以编写你的循环逻辑
        SELECT i;
        SET i = i + 1;
    END WHILE;
END$$
DELIMITER ;

CALL for_loop_example();

In the example above, a stored procedure called “for_loop_example” was created, which uses a WHILE loop to simulate a for loop and output the value of i from 1 to 10 sequentially. The loop logic can be executed by calling the stored procedure using CALL for_loop_example(); You can modify the loop logic within the stored procedure according to your needs.

bannerAds