For checking a uniform condition you may use the CASE construct : CASE « Procedure Function « MySQL Tutorial






CASE case_value
    WHEN when_value THEN statement_list
    [WHEN when_value THEN statement_list] ...
    [ELSE statement_list]
END CASE

Or:

CASE
    WHEN search_condition THEN statement_list
    [WHEN search_condition THEN statement_list] ...
    [ELSE statement_list]
END CASE
mysql>

mysql>
mysql>
mysql> DELIMITER //
mysql> CREATE FUNCTION myFunction (delivery_day INT(1)) RETURNS INT(2)
    -> BEGIN
    ->
    -> DECLARE shipping_cost INT(2) DEFAULT 0;
    ->
    -> CASE delivery_day
    -> WHEN 1 THEN
    ->         SET shipping_cost = 20;
    -> WHEN 2 THEN
    ->         SET shipping_cost = 15;
    -> WHEN 3 THEN
    ->         SET shipping_cost = 10;
    -> ELSE
    ->         SET shipping_cost = 5;
    -> END CASE;
    -> RETURN shipping_cost;
    ->
    -> END
    -> //
Query OK, 0 rows affected (0.00 sec)

mysql> DELIMITER ;
mysql>
mysql> select myFunction(1);
+---------------+
| myFunction(1) |
+---------------+
|            20 |
+---------------+
1 row in set (0.00 sec)

mysql>
mysql> select myFunction(2);
+---------------+
| myFunction(2) |
+---------------+
|            15 |
+---------------+
1 row in set (0.00 sec)

mysql>
mysql> select myFunction(3);
+---------------+
| myFunction(3) |
+---------------+
|            10 |
+---------------+
1 row in set (0.00 sec)

mysql>
mysql> drop function myFunction;
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql>








11.15.CASE
11.15.1.For checking a uniform condition you may use the CASE construct
11.15.2.CASE Statement with Condition Checks
11.15.3.Using CASE WHEN statement in a procedure
11.15.4.Using CASE WHEN condition statement in a procedure
11.15.5.CASE WHEN with ELSE