Rechercher dans le manuel MySQL
12.4 Control Flow Functions
CASE
value
WHEN [compare_value
] THENresult
[WHEN [compare_value
] THENresult
...] [ELSEresult
] ENDCASE WHEN [
condition
] THENresult
[WHEN [condition
] THENresult
...] [ELSEresult
] ENDThe first
CASE
syntax returns theresult
for the first
comparison that is true. The second syntax returns the result for the first condition that is true. If no comparison or condition is true, the result aftervalue
=compare_value
ELSE
is returned, orNULL
if there is noELSE
part.NoteThe syntax of the
CASE
expression described here differs slightly from that of the SQLCASE
statement described in Section 13.6.5.1, “CASE Syntax”, for use inside stored programs. TheCASE
statement cannot have anELSE NULL
clause, and it is terminated withEND CASE
instead ofEND
.The return type of a
CASE
expression result is the aggregated type of all result values:If all types are numeric, the aggregated type is also numeric:
If at least one argument is double precision, the result is double precision.
Otherwise, if at least one argument is
DECIMAL
, the result isDECIMAL
.Otherwise, the result is an integer type (with one exception):
If all integer types are all signed or all unsigned, the result is the same sign and the precision is the highest of all specified integer types (that is,
TINYINT
,SMALLINT
,MEDIUMINT
,INT
, orBIGINT
).If there is a combination of signed and unsigned integer types, the result is signed and the precision may be higher. For example, if the types are signed
INT
and unsignedINT
, the result is signedBIGINT
.The exception is unsigned
BIGINT
combined with any signed integer type. The result isDECIMAL
with sufficient precision and scale 0.
If all types are
BIT
, the result isBIT
. Otherwise,BIT
arguments are treated similar toBIGINT
.If all types are
YEAR
, the result isYEAR
. Otherwise,YEAR
arguments are treated similar toINT
.If all types are character string (
CHAR
orVARCHAR
), the result isVARCHAR
with maximum length determined by the longest character length of the operands.If all types are character or binary string, the result is
VARBINARY
.SET
andENUM
are treated similar toVARCHAR
; the result isVARCHAR
.If all types are temporal, the result is temporal:
If all types are
GEOMETRY
, the result isGEOMETRY
.For all other type combinations, the result is
VARCHAR
.Literal
NULL
operands are ignored for type aggregation.
If
expr1
isTRUE
(
andexpr1
<> 0
),expr1
<> NULLIF()
returnsexpr2
. Otherwise, it returnsexpr3
.NoteThere is also an
IF
statement, which differs from theIF()
function described here. See Section 13.6.5.2, “IF Syntax”.If only one of
expr2
orexpr3
is explicitlyNULL
, the result type of theIF()
function is the type of the non-NULL
expression.The default return type of
IF()
(which may matter when it is stored into a temporary table) is calculated as follows:If
expr2
orexpr3
produce a string, the result is a string.If
expr2
andexpr3
are both strings, the result is case-sensitive if either string is case sensitive.If
expr2
orexpr3
produce a floating-point value, the result is a floating-point value.If
expr2
orexpr3
produce an integer, the result is an integer.
If
expr1
is notNULL
,IFNULL()
returnsexpr1
; otherwise it returnsexpr2
.- -> 1
- -> 10
- -> 10
- -> 'yes'
The default return type of
IFNULL(
is the more “general” of the two expressions, in the orderexpr1
,expr2
)STRING
,REAL
, orINTEGER
. Consider the case of a table based on expressions or where MySQL must internally store a value returned byIFNULL()
in a temporary table:- +-------+--------------+------+-----+---------+-------+
- +-------+--------------+------+-----+---------+-------+
- +-------+--------------+------+-----+---------+-------+
In this example, the type of the
test
column isVARBINARY(4)
(a string type).Returns
NULL
if
is true, otherwise returnsexpr1
=expr2
expr1
. This is the same asCASE WHEN
.expr1
=expr2
THEN NULL ELSEexpr1
ENDThe return value has the same type as the first argument.
NoteMySQL evaluates
expr1
twice if the arguments are not equal.
Traduction non disponible
Le manuel MySQL n'est pas encore traduit en français sur l'infobrol. Seule la version anglaise est disponible pour l'instant.
Document créé le 26/06/2006, dernière modification le 26/10/2018
Source du document imprimé : https://www.gaudry.be/mysql-rf-control-flow-functions.html
L'infobrol est un site personnel dont le contenu n'engage que moi. Le texte est mis à disposition sous licence CreativeCommons(BY-NC-SA). Plus d'info sur les conditions d'utilisation et sur l'auteur.
Références
Ces références et liens indiquent des documents consultés lors de la rédaction de cette page, ou qui peuvent apporter un complément d'information, mais les auteurs de ces sources ne peuvent être tenus responsables du contenu de cette page.
L'auteur de ce site est seul responsable de la manière dont sont présentés ici les différents concepts, et des libertés qui sont prises avec les ouvrages de référence. N'oubliez pas que vous devez croiser les informations de sources multiples afin de diminuer les risques d'erreurs.