mysqleventswhile-loopdo-whilemysql-event

Writing a While Loops with Events in MySQL


I want to write a While Loop with Events in MySQL. After searching the internet, unfortunately, i didn't find something like this. However, i don't want to write it using procedures.

CREATE DEFINER=`root`@`localhost` EVENT `try` 
ON SCHEDULE EVERY 1 DAY STARTS '2023-03-10 09:00:00.000000'
ON COMPLETION NOT PRESERVE ENABLE DO 
BEGIN
    DECLARE counter int DEFAULT 1;
    WHILE counter <= 10 DO
    INSERT INTO admin(admin.admin_username, admin.admin_password, admin.admintype_id) VALUES('aaaa','bbbb',1)
    SET counter = counter + 1;
    END WHILE
END;

Solution

  • Unless this is an exercise in using a while loop in an event I think the same can be achieved with and insert..select

    INSERT INTO admintouser_cocuk(admintouser_cocuk.kullanici_cocuk_id, 
                admintouser_cocuk.admintouser_cocuk_mesaj)
    SELECT kullanici_cocuk.kullanici_cocuk_id ,
           'Sevgili evladınızın yeni yaşını kutlar, tüm ailenizle birlikte mutlu bir yaşam dileriz.'
    FROM kullanici_cocuk 
    wHERE SUBSTRING(kullanici_cocuk.cocuk_dogum_tarihi, 6, 5) = SUBSTRING(CURDATE(), 6, 5)
    ; 
    

    And I suspect using a date functions rather than subtrings would be tidier too.