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
In MySQL, the LEAVE statement is used when you want to exit a block of code identified by a label_name, such as a LOOP statement, WHILE statement, or REPEAT statement.
The syntax for this statement is as follows: [ label :] LOOP statements END LOOP [ label ]; The statements between the LOOP and END LOOP statements will be repeated indefinitely, until the LOOP is terminated. You can terminate the LOOP using the LEAVE statement, which we will describe shortly.
MySQL WHILE loop statement is used to execute one or more statements again and again, as long as a condition is true. We can use the loop when we need to execute the task with repetition while condition is true.
The MySQL stored program language offers three types of loops : Simple loops using the LOOP and END LOOP clauses. Loops that continue while a condition is true, using the WHILE and END WHILE clauses. Loops that continue until a condition is true, using the REPEAT and UNTIL clauses.
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;
You might be interested in a REPEAT
loop:
REPEAT
SET cnt = (SELECT COUNT(*) FROM temp_results WHERE result = "true");
UNTIL cnt > 0
END REPEAT;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With