Rechercher dans le manuel MySQL
9.5 Expressions
This section lists the grammar rules that expressions must follow in MySQL and provides additional information about the types of terms that may appear in expressions.
Expression Syntax
The following grammar 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. For additional information about some of the
expression terms, see Expression Term Notes.
- expr:
- expr OR expr
- | expr || expr
- | expr XOR expr
- | expr AND expr
- | expr && expr
- | NOT expr
- | ! expr
- | boolean_primary
- boolean_primary:
- | boolean_primary <=> predicate
- | boolean_primary comparison_operator predicate
- | predicate
- comparison_operator: = | >= | > | <= | < | <> | !=
- predicate:
- | bit_expr SOUNDS LIKE 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_expr
- | bit_expr / bit_expr
- | bit_expr DIV bit_expr
- | bit_expr MOD bit_expr
- | bit_expr % bit_expr
- | bit_expr ^ bit_expr
- | bit_expr + interval_expr
- | bit_expr - interval_expr
- | simple_expr
- simple_expr:
- literal
- | identifier
- | function_call
- | simple_expr COLLATE collation_name
- | param_marker
- | variable
- | simple_expr || simple_expr
- | + simple_expr
- | - simple_expr
- | ~ simple_expr
- | ! simple_expr
- | BINARY simple_expr
- | (expr [, expr] ...)
- | ROW (expr, expr [, expr] ...)
- | (subquery)
- | {identifier expr}
- | match_expr
- | case_expr
- | interval_expr
For operator precedence, see Section 12.3.1, “Operator Precedence”. The precedence and meaning of some operators depends on the SQL mode:
By default,
||
is a logicalOR
operator. WithPIPES_AS_CONCAT
enabled,||
is string concatenation, with a precedence between^
and the unary operators.By default,
!
has a higher precedence thanNOT
. WithHIGH_NOT_PRECEDENCE
enabled,!
andNOT
have the same precedence.
For literal value syntax, see Section 9.1, “Literal Values”.
For identifier syntax, see Section 9.2, “Schema Object Names”.
Variables can be user variables, system variables, or stored program local variables or parameters:
User variables: Section 9.4, “User-Defined Variables”
System variables: Section 5.1.9, “Using System Variables”
Stored program local variables: Section 13.6.4.1, “Local Variable DECLARE Syntax”
Stored program parameters: Section 13.1.17, “CREATE PROCEDURE and CREATE FUNCTION Syntax”
param_marker
is ?
as used in prepared statements for placeholders. See
Section 13.5.1, “PREPARE Syntax”.
(
indicates a subquery that returns a single value; that is, a
scalar subquery. See Section 13.2.11.1, “The Subquery as Scalar Operand”.
subquery
)
{
is ODBC escape syntax
and is accepted for ODBC compatibility. The value is
identifier
expr
}expr
. The {
and
}
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 12.9, “Full-Text Search Functions”.
case_expr
indicates a
CASE
expression. See
Section 12.4, “Control Flow Functions”.
interval_expr
represents a temporal
interval. See Temporal Intervals.
interval_expr
in expressions
represents a temporal interval. Intervals have this syntax:
- INTERVAL expr unit
expr
represents a quantity.
unit
represents the unit for
interpreting the quantity; it is a specifier such as
HOUR
, DAY
, or
WEEK
. The INTERVAL
keyword
and the unit
specifier are not case
sensitive.
The following table shows the expected form of the
expr
argument for each
unit
value.
Table 9.2 Temporal Interval Expression and Unit Arguments
unit Value |
Expected expr Format |
---|---|
MICROSECOND |
MICROSECONDS |
SECOND |
SECONDS |
MINUTE |
MINUTES |
HOUR |
HOURS |
DAY |
DAYS |
WEEK |
WEEKS |
MONTH |
MONTHS |
QUARTER |
QUARTERS |
YEAR |
YEARS |
SECOND_MICROSECOND |
'SECONDS.MICROSECONDS' |
MINUTE_MICROSECOND |
'MINUTES:SECONDS.MICROSECONDS' |
MINUTE_SECOND |
'MINUTES:SECONDS' |
HOUR_MICROSECOND |
'HOURS:MINUTES:SECONDS.MICROSECONDS' |
HOUR_SECOND |
'HOURS:MINUTES:SECONDS' |
HOUR_MINUTE |
'HOURS:MINUTES' |
DAY_MICROSECOND |
'DAYS HOURS:MINUTES:SECONDS.MICROSECONDS' |
DAY_SECOND |
'DAYS HOURS:MINUTES:SECONDS' |
DAY_MINUTE |
'DAYS HOURS:MINUTES' |
DAY_HOUR |
'DAYS HOURS' |
YEAR_MONTH |
'YEARS-MONTHS' |
MySQL permits any punctuation delimiter in the
expr
format. Those shown in the table
are the suggested delimiters.
Temporal intervals are used for certain functions, such as
DATE_ADD()
and
DATE_SUB()
:
- -> '2018-05-02'
- -> '2017-05-01'
- -> '2021-01-01 00:00:00'
- -> '2019-01-01 23:59:59'
- -> '2101-01-01 00:01:00'
- -> '2024-12-30 22:58:59'
- -> '1899-12-30 14:00:00'
- -> '1997-12-02'
- -> '1993-01-01 00:00:01.000001'
Temporal arithmetic also can be performed in expressions using
INTERVAL
together with the
+
or
-
operator:
INTERVAL
is permitted on either
side of the expr
unit
+
operator if the expression on the other side is a date or
datetime value. For the
-
operator,
INTERVAL
is permitted only on
the right side, because it makes no sense to subtract a date or
datetime value from an interval.
expr
unit
- -> '2019-01-01 00:00:00'
- -> '2019-01-01'
- -> '2024-12-31 23:59:59'
The EXTRACT()
function uses the
same kinds of unit
specifiers as
DATE_ADD()
or
DATE_SUB()
, but extracts parts
from the date rather than performing date arithmetic:
- -> 2019
- -> 201907
Temporal intervals can be used in CREATE
EVENT
statements:
- CREATE EVENT myevent
If you specify an interval value that is too short (does not
include all the interval parts that would be expected from the
unit
keyword), MySQL assumes that you
have left out the leftmost parts of the interval value. For
example, if you specify a unit
of
DAY_SECOND
, the value of
expr
is expected to have days, hours,
minutes, and seconds parts. If you specify a value like
'1:10'
, MySQL assumes that the days and hours
parts are missing and the value represents minutes and seconds.
In other words, '1:10' DAY_SECOND
is
interpreted in such a way that it is equivalent to
'1:10' MINUTE_SECOND
. This is analogous to
the way that MySQL interprets
TIME
values as representing
elapsed time rather than as a time of day.
expr
is treated as a string, so be
careful if you specify a nonstring value with
INTERVAL
. For example, with an interval
specifier of HOUR_MINUTE
, '6/4' is treated as
6 hours, four minutes, whereas 6/4
evaluates
to 1.5000
and is treated as 1 hour, 5000
minutes:
- -> 1.5000
- -> '2019-01-01 06:04:00'
- -> '2019-01-04 12:20:00'
To ensure interpretation of the interval value as you expect, a
CAST()
operation may be used. To
treat 6/4
as 1 hour, 5 minutes, cast it to a
DECIMAL
value with a single
fractional digit:
- -> 1.5
- -> '1970-01-01 13:05:00'
If you add to or subtract from a date value something that contains a time part, the result is automatically converted to a datetime value:
- -> '2023-01-02'
- -> '2023-01-01 01:00:00'
If you add MONTH
,
YEAR_MONTH
, or YEAR
and
the resulting date has a day that is larger than the maximum day
for the new month, the day is adjusted to the maximum days in
the new month:
Date arithmetic operations require complete dates and do not
work with incomplete dates such as
'2016-07-00'
or badly malformed dates:
Nederlandse vertaling
U hebt gevraagd om deze site in het Nederlands te bezoeken. Voor nu wordt alleen de interface vertaald, maar nog niet alle inhoud.Als je me wilt helpen met vertalingen, is je bijdrage welkom. Het enige dat u hoeft te doen, is u op de site registreren en mij een bericht sturen waarin u wordt gevraagd om u toe te voegen aan de groep vertalers, zodat u de gewenste pagina's kunt vertalen. Een link onderaan elke vertaalde pagina geeft aan dat u de vertaler bent en heeft een link naar uw profiel.
Bij voorbaat dank.
Document heeft de 26/06/2006 gemaakt, de laatste keer de 26/10/2018 gewijzigd
Bron van het afgedrukte document:https://www.gaudry.be/nl/mysql-rf-expressions.html
De infobrol is een persoonlijke site waarvan de inhoud uitsluitend mijn verantwoordelijkheid is. De tekst is beschikbaar onder CreativeCommons-licentie (BY-NC-SA). Meer info op de gebruiksvoorwaarden en de auteur.
Referenties
Deze verwijzingen en links verwijzen naar documenten die geraadpleegd zijn tijdens het schrijven van deze pagina, of die aanvullende informatie kunnen geven, maar de auteurs van deze bronnen kunnen niet verantwoordelijk worden gehouden voor de inhoud van deze pagina.
De auteur Deze site is als enige verantwoordelijk voor de manier waarop de verschillende concepten, en de vrijheden die met de referentiewerken worden genomen, hier worden gepresenteerd. Vergeet niet dat u meerdere broninformatie moet doorgeven om het risico op fouten te verkleinen.