Arithmetic and comparison operators
Table 3-2 shows examples of the basic arithmetic and comparison operators in SELECT statements. The basic arithmetic operators are *, +, /, and -, as well as the parentheses ( ) to control the order of evaluation of an expression.
Table 3-2. Using the arithmetic and comparison operators
| Statement | Output |
|---|---|
SELECT 8+3*2; |
14 |
SELECT (8+3)*2; |
22 |
SELECT 2=2; |
1 |
SELECT 1!=2; |
1 |
SELECT 2<=2; |
1 |
SELECT 3<=2; |
0 |
The comparison operators include =, !=, <, >, <=, and >=. Four examples are shown in Table 3-2. If an expression evaluates as true, the output is 1; if an expression evaluates as false, the output is 0. To test for equality, a single equals sign is used; this contrasts with PHP, where the double equals (==) is used for equality tests, and a single equals sign is used for assignment.
To test whether two items are equal, the != operator is provided. Less-than-or-equal-to is represented by <=, and greater-than-or-equal-to is represented by >=. Parentheses can explicitly express the evaluation order.