英文:
Writing a While Loops with Events in MySQL
问题
我想在MySQL中使用事件编写一个带有事件的While循环。在搜索互联网后,不幸的是,我没有找到类似的内容。然而,我不想使用存储过程来编写它。
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;
英文:
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;
答案1
得分: 1
除非这是一个在事件中使用while循环的练习,否则我认为可以通过使用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) ; 这样也可以实现相同的效果。而且我怀疑使用日期函数而不是子字符串会更整洁。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论