IF : IF statement « Procedure Function « MySQL Tutorial






IF search_condition THEN statement_list
    [ELSEIF search_condition THEN statement_list] ...
    [ELSE statement_list]
END IF

If statement checks a condition, running the statements in the block if the condition is true.

You can add ELSEIF statements to continue attempting to match conditions.

If desired, include a final ELSE statement.

mysql>
mysql>
mysql> DELIMITER //
mysql> CREATE FUNCTION delivery_day_shipping (delivery_day INT(1)) RETURNS INT(2)
    -> BEGIN
    ->
    -> DECLARE shipping_cost INT(2) DEFAULT 0;
    ->
    -> IF delivery_day = 1 THEN
    ->         SET shipping_cost = 20;
    -> ELSEIF delivery_day = 2 THEN
    ->         SET shipping_cost = 15;
    -> ELSEIF delivery_day = 3 THEN
    ->         SET shipping_cost = 10;
    -> ELSE
    ->         SET shipping_cost = 5;
    -> END IF;
    ->
    -> RETURN shipping_cost;
    ->
    -> END
    -> //
ERROR 1304 (42000): FUNCTION delivery_day_shipping already exists
mysql> DELIMITER ;
mysql>
mysql>








11.13.IF statement
11.13.1.IF
11.13.2.If statement with ELSEIF and ELSE
11.13.3.Using OR in an IF statement
11.13.4.Check version with if statement
11.13.5.IF statement in a LOOP statement