PolarDB-X supports logical, arithmetic, comparison, bitwise, and assignment operators. This page documents each operator type and the operator precedence rules that govern expression evaluation.
On this page:
Logical operators
| Operator | Description |
|---|---|
AND, && | Logical AND |
NOT, ! | Logical NOT |
||, OR | Logical OR |
XOR | Logical XOR |
Arithmetic operators
| Operator | Description |
|---|---|
/, DIV | Division |
%, MOD | Returns the remainder when one number is divided by another |
+ | Addition |
* | Multiplication |
- | Subtraction |
Comparison operators
Comparison operators are commonly used in conditional SELECT statements to filter rows. Each comparison returns one of three values:
1— the comparison is true0— the comparison is falseNULL— the result is unknown (typically when one or both operands are NULL)
| Operator | Description |
|---|---|
= | Equal to |
<>, != | Not equal to |
> | Greater than |
< | Less than |
<= | Less than or equal to |
>= | Greater than or equal to |
BETWEEN | True if the value is within the range, inclusive: value >= min AND value <= max |
NOT BETWEEN | True if the value is outside the range |
IN | True if the value is in a set |
NOT IN | True if the value is not in a set |
<=> | NULL-safe equality: returns 1 if both operands are NULL, returns 0 if one operand is NULL |
LIKE | Pattern matching using % and _ wildcards |
REGEXP, RLIKE | Pattern matching using regular expressions |
IS NULL | True if the value is NULL |
IS NOT NULL | True if the value is not NULL |
The ordinary equality operator (=) returns NULL when either operand is NULL, which may produce unexpected results inWHEREclauses. Use<=>when comparing columns that may contain NULL values.
Bitwise operators
| Operator | Description |
|---|---|
& | Bitwise AND |
~ | Bitwise NOT |
| | Bitwise OR |
^ | Bitwise XOR |
<< | Left shift |
>> | Right shift |
Assignment operators
PolarDB-X supports the = assignment operator, used primarily in the SET clause of UPDATE statements.
PolarDB-X does not support the := assignment operator.
Operator precedence
The following table lists operators in descending order of precedence (highest first). Operators at the same precedence level are evaluated left to right.
| Precedence | Operator | Type |
|---|---|---|
| 15 | ! | Unary (prefix) |
| 14 | - (unary minus), ~ | Unary (prefix) |
| 13 | ^ | Binary |
| 12 | *, /, %, MOD | Binary |
| 11 | +, - | Binary |
| 10 | <<, >> | Binary |
| 9 | & | Binary |
| 8 | | | Binary |
| 7 | = (comparison), <=>, >, >=, <, <=, <>, !=, IS, LIKE, REGEXP, IN | Binary |
| 6 | BETWEEN | Ternary |
| 5 | NOT | Unary (prefix) |
| 4 | AND, && | Binary |
| 3 | XOR | Binary |
| 2 | OR, || | Binary |
| 1 | = (assignment) | Binary |
IN and NOT IN precedence
IN and NOT IN have higher precedence than the = comparison operator. The following examples, run on MySQL 5.7.19, demonstrate this behavior. PolarDB-X follows the same precedence rule.
SELECT BINARY 'a' = 'a' IN (1, 2, 3);Result:
+-------------------------------+
| binary 'a' = 'a' in (1, 2, 3) |
+-------------------------------+
| 1 |
+-------------------------------+
1 row in set, 1 warning (0.01 sec)The expression is evaluated as BINARY 'a' = ('a' IN (1, 2, 3)), not (BINARY 'a' = 'a') IN (1, 2, 3), because IN has higher precedence than =.
SELECT 1 IN (1, 2, 3) = 'a';Result:
+----------------------+
| 1 in (1, 2, 3) = 'a' |
+----------------------+
| 0 |
+----------------------+
1 row in set, 1 warning (0.00 sec)Both queries produce Warning 1292: Truncated incorrect DOUBLE value: 'a'.
MySQL compatibility
PolarDB-X is compatible with MySQL operator syntax with the following exception:
| Operator | MySQL support | PolarDB-X support |
|---|---|---|
:= (assignment) | Supported | Not supported |