The following rules define expression syntax in MySQL. The grammar
shown here is based on that given in the
sql/sql_yacc.yy file of MySQL source
distributions. See the notes after the grammar for additional
information about some of the terms. Operator precedence is given
in Section 11.3.1, “Operator Precedence”.
expr:exprORexpr|expr||expr|exprXORexpr|exprANDexpr|expr&&expr| NOTexpr| !expr|boolean_primaryIS [NOT] {TRUE | FALSE | UNKNOWN} |boolean_primaryboolean_primary:boolean_primaryIS [NOT] NULL |boolean_primary<=>predicate|boolean_primarycomparison_operatorpredicate|boolean_primarycomparison_operator{ALL | ANY} (subquery) |predicatecomparison_operator: = | >= | > | <= | < | <> | !=predicate:bit_expr[NOT] IN (subquery) |bit_expr[NOT] IN (expr[,expr] ...) |bit_expr[NOT] BETWEENbit_exprANDpredicate|bit_exprSOUNDS LIKEbit_expr|bit_expr[NOT] LIKEsimple_expr[ESCAPEsimple_expr] |bit_expr[NOT] REGEXPbit_expr|bit_exprbit_expr:bit_expr|bit_expr|bit_expr&bit_expr|bit_expr<<bit_expr|bit_expr>>bit_expr|bit_expr+bit_expr|bit_expr-bit_expr|bit_expr*bit_expr|bit_expr/bit_expr|bit_exprDIVbit_expr|bit_exprMODbit_expr|bit_expr%bit_expr|bit_expr^bit_expr|bit_expr+interval_expr|bit_expr-interval_expr|simple_exprsimple_expr:literal|identifier|function_call|simple_exprCOLLATEcollation_name|param_marker|variable|simple_expr||simple_expr| +simple_expr| -simple_expr| ~simple_expr| !simple_expr| BINARYsimple_expr| (expr[,expr] ...) | ROW (expr,expr[,expr] ...) | (subquery) | EXISTS (subquery) | {identifierexpr} |match_expr|case_expr|interval_expr
Notes:
For literal value syntax, see Section 8.1, “Literal Values”.
For identifier syntax, see Section 8.2, “Schema Object Names”.
Variables can be user variables, system variables, or stored program local variables or parameters:
User variables: Section 8.4, “User-Defined Variables”
System variables: Section 5.1.5, “Using System Variables”
Local variables: Section 12.7.3.1, “
DECLAREfor Local Variables”Parameters: Section 12.1.12, “
CREATE PROCEDUREandCREATE FUNCTIONSyntax”
param_marker is '?'
as used in prepared statements for placeholders. See
Section 12.6.1, “PREPARE Syntax”.
( indicates
a subquery that returns a single value; that is, a scalar
subquery. See Section 12.2.10.1, “The Subquery as Scalar Operand”.
subquery)
{ is ODBC escape syntax
and is accepted for ODBC compatibility. The value is
identifier
expr}expr. The curly braces in the syntax
should be written literally; they are not metasyntax as used
elsewhere in syntax descriptions.
match_expr indicates a
MATCH expression. See
Section 11.9, “Full-Text Search Functions”.
case_expr indicates a
CASE expression. See
Section 11.4, “Control Flow Functions”.
interval_expr represents a time
interval. The syntax is INTERVAL
, where
expr
unitunit is a specifier such as
HOUR, DAY, or
WEEK. For the full list of
unit specifiers, see the description of
the DATE_ADD() function in
Section 11.7, “Date and Time Functions”.
The meaning of some operators depends on the SQL mode:
By default,
||is a logicalORoperator. WithPIPES_AS_CONCATenabled,||is string concatenation, with a precedence between^and the unary operators.By default,
!has a higher precedence thanNOT. WithHIGH_NOT_PRECEDENCEenabled,!andNOThave the same precedence.