~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to docs/functions/control_flow.rst

Merge Monty - Added inter-plugin dependencies for controlling plugin load order

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
Control Flow Functions
2
 
======================
3
 
 
4
 
There are four control flow functions: 
5
 
 
6
 
* CASE
7
 
* IF/ELSE
8
 
* IFNULL
9
 
* NULLIF
10
 
 
11
 
Control flow functions return a value for each row processed, which represents the result of the comparison or condition specified. They can be used in ``SELECT``, ``WHERE``, ``ORDER BY``, and ``GROUP BY`` statements.
12
 
 
13
 
CASE
14
 
----
15
 
 
16
 
There are two basic examples of the ``CASE`` statment:
17
 
 
18
 
 1. ::
19
 
 
20
 
        CASE value WHEN [compare_value] THEN result [WHEN [compare_value] THEN result ...] [ELSE result] END
21
 
 
22
 
In this version, result is returned when value is equal to compare_value. If nothing is matched, the result after ``ELSE`` is returned, or ``NULL`` is returned if there is no ``ELSE`` part.
23
 
 
24
 
 2. ::
25
 
 
26
 
        CASE WHEN [condition] THEN result [WHEN [condition] THEN result ...] [ELSE result] END
27
 
 
28
 
In this version, if [condition] is true, result is returned. If nothing is matched, the result after ``ELSE`` is returned, or ``NULL`` is returned if there is no ``ELSE`` part.
29
 
 
30
 
When [condition] is for equal comparison (=), this example syntax returns the same result as the first example.
31
 
 
32
 
IF/ELSE
33
 
---------
34
 
 
35
 
This type of control flow function checks IF a condition is satisfied (i.e. the Boolean expression returns TRUE), the IF SQL statement will execute. IF the condition is not satisfied (i.e. the Boolean expression returns FALSE), the ELSE SQL statement is executed.
36
 
 
37
 
Syntax for IF/ELSE: ::
38
 
 
39
 
        IF ( Boolean_expression ) 
40
 
        BEGIN
41
 
        SQL statement
42
 
        END
43
 
        ELSE
44
 
        BEGIN
45
 
        SQL statement
46
 
        END
47
 
 
48
 
 
49
 
IFNULL
50
 
-------
51
 
 
52
 
Given IFNULL(arg1,arg2), if arg1 is not NULL, IFNULL() returns arg1; it otherwise returns arg2. IFNULL() returns a numeric or string value, depending on how it's used. ::
53
 
 
54
 
.. code-block:: mysql
55
 
 
56
 
        SELECT IFNULL(2,0);
57
 
        -> 2
58
 
        
59
 
        SELECT IFNULL(NULL,1);
60
 
        -> 1
61
 
        
62
 
        SELECT IFNULL(1/0,10);
63
 
        -> 10
64
 
        
65
 
        SELECT IFNULL(1/0,'yes');
66
 
        -> 'yes'
67
 
 
68
 
NULLIF
69
 
-------
70
 
 
71
 
NULLIF(arg1,arg2) returns NULL if arg1 = arg2 is true, otherwise returns arg1. ::
72
 
 
73
 
.. code-block:: mysql
74
 
 
75
 
        SELECT NULLIF(1,1);
76
 
        -> NULL
77
 
 
78
 
        SELECT NULLIF(1,2);
79
 
        -> 1