12.4 Operators

Operators

Name Description Introduced

'&'

Bitwise AND

'>'

Greater than operator

'>>'

Right shift

'>='

Greater than or equal operator

'<'

Less than operator

'<>', '!='

Not equal operator

'<<'

Left shift

'<='

Less than or equal operator

'<=>'

NULL-safe equal to operator

'%', 'MOD'

Modulo operator

'*'

Multiplication operator

'+'

Addition operator

'-'

Minus operator

'-'

Change the sign of the argument

'->'

Return value from JSON column after evaluating path; equivalent to JSON_EXTRACT().

'->>'

Return value 5.7.13 from JSON column
after evaluating path and unquoting the result; equivalent to JSON_UNQUOTE(JSON_EXTRACT()).

'/'

Division operator

':='

Assign a value

'='

Assign a value (as part of a note 'SET': set-variable. statement, or as part of the 'SET' clause in an note 'UPDATE': update. statement)

'='

Equal operator

'^'

Bitwise XOR

'AND', '&&'

Logical AND

'BETWEEN ... AND ...'

Whether a value is within a range of values

'BINARY'

Cast a string to a binary string

'CASE'

Case operator

'DIV'

Integer division

'EXISTS()'

Whether the result of a query contains any rows

'IN()'

Whether a value is within a set of values

'IS'

Test a value against a boolean

'IS NOT'

Test a value against a boolean

'IS NOT NULL'

NOT NULL value test

'IS NULL'

NULL value test

'LIKE'

Simple pattern matching

'NOT', '!'

Negates value

'NOT BETWEEN ... AND ...'

Whether a value is not within a range of values

'NOT EXISTS()'

Whether the result of a query contains no rows

'NOT IN()'

Whether a value is not within a set of values

'NOT LIKE'

Negation of simple pattern matching

'NOT REGEXP'

Negation of REGEXP

'OR', '||'

Logical OR

'REGEXP'

Whether string matches regular expression

'RLIKE'

Whether string matches regular expression

'SOUNDS LIKE'

Compare sounds

'XOR'

Logical XOR

'|'

Bitwise OR

'~'

Bitwise inversion

 File: manual.info.tmp, Node: operator-precedence, Next: comparison-operators, Prev: non-typed-operators, Up: non-typed-operators

12.4.1 Operator Precedence

Operator precedences are shown in the following list, from highest precedence to the lowest. Operators that are shown together on a line have the same precedence.

 INTERVAL
 BINARY, COLLATE
 !
 - (unary minus), ~ (unary bit inversion)
 ^
 *, /, DIV, %, MOD
 -, +
 <<, >>
 &
 |
 = (comparison), <=>, >=, >, <=, <, <>, !=, IS, LIKE, REGEXP, IN
 BETWEEN, CASE, WHEN, THEN, ELSE
 NOT
 AND, &&
 XOR
 OR, ||
 = (assignment), :=

The precedence of '=' depends on whether it is used as a comparison operator ('=') or as an assignment operator ('='). When used as a comparison operator, it has the same precedence as '<=>', '>=', '>', '<=', '<', '<>', '!=', 'IS', 'LIKE', 'REGEXP', and 'IN()'. When used as an assignment operator, it has the same precedence as ':='. note set-variable::, and note user-variables::, explain how MySQL determines which interpretation of '=' should apply.

For operators that occur at the same precedence level within an expression, evaluation proceeds left to right, with the exception that assignments evaluate right to left.

The precedence and meaning of some operators depends on the SQL mode:

See *note sql-mode::.

The precedence of operators determines the order of evaluation of terms in an expression. To override this order and group terms explicitly, use parentheses. For example:

 mysql> SELECT 1+2*3;
         -> 7
 mysql> SELECT (1+2)*3;
         -> 9

 File: manual.info.tmp, Node: comparison-operators, Next: logical-operators, Prev: operator-precedence, Up: non-typed-operators

12.4.2 Comparison Functions and Operators

Comparison Operators

Name Description

'>' Greater than operator

'>=' Greater than or equal operator

'<' Less than operator

'<>', '!=' Not equal operator

'<=' Less than or equal operator

'<=>' NULL-safe equal to operator

'=' Equal operator

'BETWEEN ... AND Whether a value is within a range of values ...'

'COALESCE()' Return the first non-NULL argument

'EXISTS()' Whether the result of a query contains any rows

'GREATEST()' Return the largest argument

'IN()' Whether a value is within a set of values

'INTERVAL()' Return the index of the argument that is less than the first argument

'IS' Test a value against a boolean

'IS NOT' Test a value against a boolean

'IS NOT NULL' NOT NULL value test

'IS NULL' NULL value test

'ISNULL()' Test whether the argument is NULL

'LEAST()' Return the smallest argument

'LIKE' Simple pattern matching

'NOT BETWEEN ... Whether a value is not within a range of values AND ...'

'NOT EXISTS()' Whether the result of a query contains no rows

'NOT IN()' Whether a value is not within a set of values

'NOT LIKE' Negation of simple pattern matching

'STRCMP()' Compare two strings

Comparison operations result in a value of '1' ('TRUE'), '0' ('FALSE'), or 'NULL'. These operations work for both numbers and strings. Strings are automatically converted to numbers and numbers to strings as necessary.

The following relational comparison operators can be used to compare not only scalar operands, but row operands:

 =  >  <  >=  <=  <>  !=

The descriptions for those operators later in this section detail how they work with row operands. For additional examples of row comparisons in the context of row subqueries, see *note row-subqueries::.

Some of the functions in this section return values other than '1' ('TRUE'), '0' ('FALSE'), or 'NULL'. 'LEAST()' and 'GREATEST()' are examples of such functions; *note type-conversion::, describes the rules for comparison operations performed by these and similar functions for determining their return values.

To convert a value to a specific type for comparison purposes, you can use the 'CAST()' function. String values can be converted to a different character set using 'CONVERT()'. See *note cast-functions::.

By default, string comparisons are not case-sensitive and use the current character set. The default is 'latin1' (cp1252 West European), which also works well for English.

 File: manual.info.tmp, Node: logical-operators, Next: assignment-operators, Prev: comparison-operators, Up: non-typed-operators

12.4.3 Logical Operators

Logical Operators

Name Description

'AND', '&&' Logical AND

'NOT', '!' Negates value

'OR', '||' Logical OR

'XOR' Logical XOR

In SQL, all logical operators evaluate to 'TRUE', 'FALSE', or 'NULL' ('UNKNOWN'). In MySQL, these are implemented as 1 ('TRUE'), 0 ('FALSE'), and 'NULL'. Most of this is common to different SQL database servers, although some servers may return any nonzero value for 'TRUE'.

MySQL evaluates any nonzero, non-'NULL' value to 'TRUE'. For example, the following statements all assess to 'TRUE':

 mysql> SELECT 10 IS TRUE;
 -> 1
 mysql> SELECT -10 IS TRUE;
 -> 1
 mysql> SELECT 'string' IS NOT NULL;
 -> 1

 File: manual.info.tmp, Node: assignment-operators, Prev: logical-operators, Up: non-typed-operators

12.4.4 Assignment Operators

Assignment Operators

Name Description

':=' Assign a value

'=' Assign a value (as part of a note 'SET': set-variable. statement, or as part of the 'SET' clause in an note 'UPDATE': update. statement)

 File: manual.info.tmp, Node: flow-control-functions, Next: numeric-functions, Prev: non-typed-operators, Up: functions