SELECTOrderID,Quantity,CASEWHENQuantity>30THEN"The quantity is greater than 30"WHENQuantity=30THEN"The quantity is 30"ELSE"The quantity is something else"ENDFROMOrderDetails;
CASE WHEN is usually used in SELECT statement, but it's possible to be used in other statements. Here is a example:
IF Syntax
IF statement works similar to CASE WHEN. In practice, we use CASE WHEN more often.
LOOP Syntax
LOOPimplements a simple loop construct, enabling repeated execution of the statement list, which consists of one or more statements, each terminated by a semicolon (;) statement delimiter. The statements within the loop are repeated until the loop is terminated. Usually, this is accomplished with aLEAVEstatement. Within a stored function,RETURNcan also be used, which exits the function entirely.
Neglecting to include a loop-termination statement results in an infinite loop.
The statement list within aWHILEstatement is repeated as long as thesearch_condition_expression is true.statement_list_consists of one or more SQL statements, each terminated by a semicolon (;) statement delimiter.
The statement list within aREPEATstatement is repeated until thesearch_condition_expression is true. Thus, aREPEATalways enters the loop at least once.statement_list_consists of one or more statements, each terminated by a semicolon (;) statement delimiter.
TheRETURNstatement terminates execution of a stored function and returns the value_expr_to the function caller. There must be at least oneRETURNstatement in a stored function. There may be more than one if the function has multiple exit points.
This statement is not used in stored procedures, triggers, or events. TheLEAVEstatement can be used to exit a stored program of those types.
This statement is used to exit the flow control construct that has the given label. If the label is for the outermost stored program block,LEAVEexits the program.
# order the customers by City, if City is NULL, then order by Country
SELECT CustomerName, City, Country
FROM Customers
ORDER BY
(CASE
WHEN City IS NULL THEN Country
ELSE City
END);
IF search_condition THEN statement_list
[ELSEIF search_condition THEN statement_list] ...
[ELSE statement_list]
END;
[begin_label:] LOOP
statement_list
END LOOP [end_label]
CREATE PROCEDURE doiterate(p1 INT)
BEGIN
label1: LOOP
SET p1 = p1 + 1;
IF p1 < 10 THEN
ITERATE label1;
END IF;
LEAVE label1;
END LOOP label1;
SET @x = p1;
END;
[begin_label:] WHILE search_condition DO
statement_list
END WHILE [end_label]
CREATE PROCEDURE dowhile()
BEGIN
DECLARE v1 INT DEFAULT 5;
WHILE v1 > 0 DO
...
SET v1 = v1 - 1;
END WHILE;
END;
[begin_label:] REPEAT
statement_list
UNTIL search_condition
END REPEAT [end_label]
mysql> delimiter //
mysql> CREATE PROCEDURE dorepeat(p1 INT)
-> BEGIN
-> SET @x = 0;
-> REPEAT
-> SET @x = @x + 1;
-> UNTIL @x > p1 END REPEAT;
-> END
-> //
Query OK, 0 rows affected (0.00 sec)
mysql> CALL dorepeat(1000)//
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT @x//
+------+
| @x |
+------+
| 1001 |
+------+
1 row in set (0.00 sec)