MySQL 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.
Examples using the arithmetic and comparison operators
SELECT 8+3*2;
Output: 14
SELECT (8+3)*2;
Output: 22
SELECT 2=2;
Output: 1
SELECT 1!=2;
Output: 1
SELECT 2<=2;
Output: 1
SELECT 3<=2;
Ouput: 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.