Rechercher dans le manuel MySQL
12.4 Control Flow Functions
CASEvalueWHEN [compare_value] THENresult[WHEN [compare_value] THENresult...] [ELSEresult] ENDCASE WHEN [condition] THENresult[WHEN [condition] THENresult...] [ELSEresult] ENDThe first
CASEsyntax returns theresultfor the firstcomparison 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_valueELSEis returned, orNULLif there is noELSEpart.NoteThe syntax of the
CASEexpr described here differs slightly from that of the SQLCASEstatement described in Section 13.6.5.1, “CASE Syntax”, for use inside stored programs. TheCASEstatement cannot have anELSE NULLclause, and it is terminated withEND CASEinstead ofEND.The return type of a
CASEexpression 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
INTand unsignedINT, the result is signedBIGINT.The exception is unsigned
BIGINTcombined with any signed integer type. The result isDECIMALwith sufficient precision and scale 0.
If all types are
BIT, the result isBIT. Otherwise,BITarguments are treated similar toBIGINT.If all types are
YEAR, the result isYEAR. Otherwise,YEARarguments are treated similar toINT.If all types are character string (
CHARorVARCHAR), the result isVARCHARwith maximum length determined by the longest character length of the operands.If all types are character or binary string, the result is
VARBINARY.SETandENUMare 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
NULLoperands are ignored for type aggregation.
If
expr1isTRUE(andexpr1<> 0),expr1<> NULLIF()returnsexpr2. Otherwise, it returnsexpr3.NoteThere is also an
IFstatement, which differs from theIF()function described here. See Section 13.6.5.2, “IF Syntax”.If only one of
expr2orexpr3is explicitlyNULL, the result type of theIF()function is the type of the non-NULLexpression.The default return type of
IF()(which may matter when it is stored into a temporary table) is calculated as follows:If
expr2orexpr3produce a string, the result is a string.If
expr2andexpr3are both strings, the result is case-sensitive if either string is case sensitive.If
expr2orexpr3produce a floating-point value, the result is a floating-point value.If
expr2orexpr3produce an integer, the result is an integer.
If
expr1is 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
testcolumn isVARBINARY(4)(a string type).Returns
NULLifis true, otherwise returnsexpr1=expr2expr1. This is the same asCASE WHEN.expr1=expr2THEN NULL ELSEexpr1ENDThe return value has the same type as the first argument.
NoteMySQL evaluates
expr1twice if the arguments are not equal.
Deutsche Übersetzung
Sie haben gebeten, diese Seite auf Deutsch zu besuchen. Momentan ist nur die Oberfläche übersetzt, aber noch nicht der gesamte Inhalt.Wenn Sie mir bei Übersetzungen helfen wollen, ist Ihr Beitrag willkommen. Alles, was Sie tun müssen, ist, sich auf der Website zu registrieren und mir eine Nachricht zu schicken, in der Sie gebeten werden, Sie der Gruppe der Übersetzer hinzuzufügen, die Ihnen die Möglichkeit gibt, die gewünschten Seiten zu übersetzen. Ein Link am Ende jeder übersetzten Seite zeigt an, dass Sie der Übersetzer sind und einen Link zu Ihrem Profil haben.
Vielen Dank im Voraus.
Dokument erstellt 26/06/2006, zuletzt geändert 26/10/2018
Quelle des gedruckten Dokuments:https://www.gaudry.be/de/mysql-rf-control-flow-functions.html
Die Infobro ist eine persönliche Seite, deren Inhalt in meiner alleinigen Verantwortung liegt. Der Text ist unter der CreativeCommons-Lizenz (BY-NC-SA) verfügbar. Weitere Informationen auf die Nutzungsbedingungen und dem Autor.
Referenzen
Diese Verweise und Links verweisen auf Dokumente, die während des Schreibens dieser Seite konsultiert wurden, oder die zusätzliche Informationen liefern können, aber die Autoren dieser Quellen können nicht für den Inhalt dieser Seite verantwortlich gemacht werden.
Der Autor dieser Website ist allein dafür verantwortlich, wie die verschiedenen Konzepte und Freiheiten, die mit den Nachschlagewerken gemacht werden, hier dargestellt werden. Denken Sie daran, dass Sie mehrere Quellinformationen austauschen müssen, um das Risiko von Fehlern zu reduzieren.