IF : IF statement « Procedure Function « MySQL Tutorial

Home
MySQL Tutorial
1.Introduction
2.Select Query
3.Database
4.Table
5.Table Join
6.Subquery
7.Insert Update Delete
8.Logic Operator
9.View
10.Data Types
11.Procedure Function
12.Cursor
13.Trigger
14.Date Time Functions
15.Comparison Functions Operators
16.Aggregate Functions
17.Cast Functions Operators
18.Control Flow Functions
19.Encryption Compression Functions
20.Information Functions
21.Math Numeric Functions
22.Miscellaneous Functions
23.String Functions
24.Regular Expressions
25.Data Dictionary
26.MySQL Utilities
27.Privilege
MySQL Tutorial » Procedure Function » IF statement 
11.13.1.IF
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(2DEFAULT 0;
    ->
    -> IF delivery_day = THEN
    ->         SET shipping_cost = 20;
    -> ELSEIF delivery_day = THEN
    ->         SET shipping_cost = 15;
    -> ELSEIF delivery_day = 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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.