【问题标题】:mysql while loop Break equivalentmysql while循环中断等效
【发布时间】:2011-11-14 16:55:29
【问题描述】:

什么相当于 mysql 的 while 循环中的中断?

  WHILE (ctr < i)
  DO ......

    SET cnt = (SELECT COUNT(*) FROM temp_results WHERE result = "true");
    IF cnt > 0 THEN
      SELECT cnt;
      BREAK;
    END IF;

谢谢

【问题讨论】:

    标签: mysql stored-procedures


    【解决方案1】:

    知道了。

    myloop: WHILE (ctr < i)
    DO 
       …
    
       SET cnt = (SELECT COUNT(*) FROM temp_results WHERE result = "true");
       IF cnt > 0 THEN
          SELECT cnt;
          LEAVE myloop;
       END IF;
    END WHILE myloop;
    

    【讨论】:

    • 我已经更新了这个例子。要使用 LEAVE 语句,我发现您需要在 END WHILE 语句上重复使用 myloop 标签。
    【解决方案2】:

    您可能对REPEAT 循环感兴趣:

    REPEAT  
        SET cnt = (SELECT COUNT(*) FROM temp_results WHERE result = "true");
    UNTIL cnt > 0 
    END REPEAT;
    

    【讨论】:

    • 这应该设置为正确的方式。从未听说过REPEAT,刚刚测试过,完美无瑕。谢谢@p.campbell
    猜你喜欢
    • 2019-06-03
    • 1970-01-01
    • 2015-04-17
    • 2017-11-14
    • 2019-05-05
    • 2022-01-12
    • 1970-01-01
    • 2015-07-18
    相关资源
    最近更新 更多