使用MySQL编写带有事件的While循环

huangapple go评论107阅读模式
英文:

Writing a While Loops with Events in MySQL

问题

  1. 我想在MySQL中使用事件编写一个带有事件的While循环。在搜索互联网后,不幸的是,我没有找到类似的内容。然而,我不想使用存储过程来编写它。
  1. CREATE DEFINER=`root`@`localhost` EVENT `try`
  2. ON SCHEDULE EVERY 1 DAY STARTS '2023-03-10 09:00:00.000000'
  3. ON COMPLETION NOT PRESERVE ENABLE DO
  4. BEGIN
  5. DECLARE counter int DEFAULT 1;
  6. WHILE counter <= 10 DO
  7. INSERT INTO admin(admin.admin_username, admin.admin_password, admin.admintype_id) VALUES('aaaa','bbbb',1)
  8. SET counter = counter + 1;
  9. END WHILE
  10. 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.

  1. CREATE DEFINER=`root`@`localhost` EVENT `try`
  2. ON SCHEDULE EVERY 1 DAY STARTS &#39;2023-03-10 09:00:00.000000&#39;
  3. ON COMPLETION NOT PRESERVE ENABLE DO
  4. BEGIN
  5. DECLARE counter int DEFAULT 1;
  6. WHILE counter &lt;= 10 DO
  7. INSERT INTO admin(admin.admin_username, admin.admin_password, admin.admintype_id) VALUES(&#39;aaaa&#39;,&#39;bbbb&#39;,1)
  8. SET counter = counter + 1;
  9. END WHILE
  10. 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

  1. INSERT INTO admintouser_cocuk(admintouser_cocuk.kullanici_cocuk_id,
  2. admintouser_cocuk.admintouser_cocuk_mesaj)
  3. SELECT kullanici_cocuk.kullanici_cocuk_id ,
  4. &#39;Sevgili evladınızın yeni yaşını kutlar, t&#252;m ailenizle birlikte mutlu bir yaşam dileriz.&#39;
  5. FROM kullanici_cocuk
  6. wHERE SUBSTRING(kullanici_cocuk.cocuk_dogum_tarihi, 6, 5) = SUBSTRING(CURDATE(), 6, 5)
  7. ;

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

huangapple
  • 本文由 发表于 2023年3月9日 19:16:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75683858.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定