23

What would be an equivalent of a break in a while loop for mysql?

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

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

Thanks

2 Answers 2

43

got it.

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;
Sign up to request clarification or add additional context in comments.

1 Comment

I've updated this example. To use the LEAVE statement I found you required the myloop label repeated on the END WHILE statement.
19

You might be interested in a REPEAT loop:

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

1 Comment

This should be set as the correct way. Never even heard of the REPEAT, just tested it, flawless. Thanks @p.campbell

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.