Shah
Shah

Reputation: 5018

mysql while loop Break equivalent

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

Upvotes: 22

Views: 54702

Answers (2)

Shah
Shah

Reputation: 5018

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;

Upvotes: 43

p.campbell
p.campbell

Reputation: 100667

You might be interested in a REPEAT loop:

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

Upvotes: 19

Related Questions