Rechercher dans le manuel MySQL
12.6.2 Mathematical Functions
Table 12.12 Mathematical Functions
Name | Description |
---|---|
ABS() |
Return the absolute value |
ACOS() |
Return the arc cosine |
ASIN() |
Return the arc sine |
ATAN() |
Return the arc tangent |
ATAN2() , ATAN() |
Return the arc tangent of the two arguments |
CEIL() |
Return the smallest integer value not less than the argument |
CEILING() |
Return the smallest integer value not less than the argument |
CONV() |
Convert numbers between different number bases |
COS() |
Return the cosine |
COT() |
Return the cotangent |
CRC32() |
Compute a cyclic redundancy check value |
DEGREES() |
Convert radians to degrees |
EXP() |
Raise to the power of |
FLOOR() |
Return the largest integer value not greater than the argument |
LN() |
Return the natural logarithm of the argument |
LOG() |
Return the natural logarithm of the first argument |
LOG10() |
Return the base-10 logarithm of the argument |
LOG2() |
Return the base-2 logarithm of the argument |
MOD() |
Return the remainder |
PI() |
Return the value of pi |
POW() |
Return the argument raised to the specified power |
POWER() |
Return the argument raised to the specified power |
RADIANS() |
Return argument converted to radians |
RAND() |
Return a random floating-point value |
ROUND() |
Round the argument |
SIGN() |
Return the sign of the argument |
SIN() |
Return the sine of the argument |
SQRT() |
Return the square root of the argument |
TAN() |
Return the tangent of the argument |
TRUNCATE() |
Truncate to specified number of decimal places |
All mathematical functions return NULL
in the
event of an error.
Returns the absolute value of
X
.This function is safe to use with
BIGINT
values.Returns the arc cosine of
X
, that is, the value whose cosine isX
. ReturnsNULL
ifX
is not in the range-1
to1
.Returns the arc sine of
X
, that is, the value whose sine isX
. ReturnsNULL
ifX
is not in the range-1
to1
.- -> 0.20135792079033
- +-------------+
- +-------------+
- | 0 |
- +-------------+
- +---------+------+-----------------------------------------+
- | Level | Code | Message |
- +---------+------+-----------------------------------------+
- +---------+------+-----------------------------------------+
Returns the arc tangent of
X
, that is, the value whose tangent isX
.Returns the arc tangent of the two variables
X
andY
. It is similar to calculating the arc tangent of
, except that the signs of both arguments are used to determine the quadrant of the result.Y
/X
Returns the smallest integer value not less than
X
.For exact-value numeric arguments, the return value has an exact-value numeric type. For string or floating-point arguments, the return value has a floating-point type.
Converts numbers between different number bases. Returns a string representation of the number
N
, converted from basefrom_base
to baseto_base
. ReturnsNULL
if any argument isNULL
. The argumentN
is interpreted as an integer, but may be specified as an integer or a string. The minimum base is2
and the maximum base is36
. Iffrom_base
is a negative number,N
is regarded as a signed number. Otherwise,N
is treated as unsigned.CONV()
works with 64-bit precision.Returns the cosine of
X
, whereX
is given in radians.Returns the cotangent of
X
.Computes a cyclic redundancy check value and returns a 32-bit unsigned value. The result is
NULL
if the argument isNULL
. The argument is expected to be a string and (if possible) is treated as one if it is not.Returns the argument
X
, converted from radians to degrees.Returns the value of e (the base of natural logarithms) raised to the power of
X
. The inverse of this function isLOG()
(using a single argument only) orLN()
.Returns the largest integer value not greater than
X
.For exact-value numeric arguments, the return value has an exact-value numeric type. For string or floating-point arguments, the return value has a floating-point type.
Formats the number
X
to a format like'#,###,###.##'
, rounded toD
decimal places, and returns the result as a string. For details, see Section 12.5, “String Functions”.This function can be used to obtain a hexadecimal representation of a decimal number or a string; the manner in which it does so varies according to the argument's type. See this function's description in Section 12.5, “String Functions”, for details.
Returns the natural logarithm of
X
; that is, the base-e logarithm ofX
. IfX
is less than or equal to 0.0E0, the function returnsNULL
and a warning “Invalid argument for logarithm” is reported.This function is synonymous with
LOG(
. The inverse of this function is theX
)EXP()
function.If called with one parameter, this function returns the natural logarithm of
X
. IfX
is less than or equal to 0.0E0, the function returnsNULL
and a warning “Invalid argument for logarithm” is reported.The inverse of this function (when called with a single argument) is the
EXP()
function.If called with two parameters, this function returns the logarithm of
X
to the baseB
. IfX
is less than or equal to 0, or ifB
is less than or equal to 1, thenNULL
is returned.LOG(
is equivalent toB
,X
)LOG(
.X
) / LOG(B
)Returns the base-2 logarithm of
. IfX
X
is less than or equal to 0.0E0, the function returnsNULL
and a warning “Invalid argument for logarithm” is reported.LOG2()
is useful for finding out how many bits a number requires for storage. This function is equivalent to the expressionLOG(
.X
) / LOG(2)Returns the base-10 logarithm of
X
. IfX
is less than or equal to 0.0E0, the function returnsNULL
and a warning “Invalid argument for logarithm” is reported.- -> 0.30102999566398
- -> 2
- -> NULL
Modulo operation. Returns the remainder of
N
divided byM
.- -> 4
- -> 1
- -> 2
- -> 2
This function is safe to use with
BIGINT
values.MOD()
also works on values that have a fractional part and returns the exact remainder after division:MOD(
returnsN
,0)NULL
.Returns the value of π (pi). The default number of decimal places displayed is seven, but MySQL uses the full double-precision value internally.
Returns the value of
X
raised to the power ofY
.This is a synonym for
POW()
.Returns the argument
X
, converted from degrees to radians. (Note that π radians equals 180 degrees.)Returns a random floating-point value
v
in the range0
<=v
<1.0
. To obtain a random integerR
in the rangei
<=R
<j
, use the expressionFLOOR(
−i
+ RAND() * (j
. For example, to obtain a random integer in the range the rangei
))7
<=R
<12
, use the following statement:If an integer argument
N
is specified, it is used as the seed value:With a constant initializer argument, the seed is initialized once when the statement is prepared, prior to execution.
With a nonconstant initializer argument (such as a column name), the seed is initialized with the value for each invocation of
RAND()
.
One implication of this behavior is that for equal argument values,
RAND(
returns the same value each time, and thus produces a repeatable sequence of column values. In the following example, the sequence of values produced byN
)RAND(3)
is the same both places it occurs.- Query OK, 0 rows affected (0.42 sec)
- Query OK, 3 rows affected (0.00 sec)
- +------+------------------+
- +------+------------------+
- | 1 | 0.61914388706828 |
- | 2 | 0.93845168309142 |
- | 3 | 0.83482678498591 |
- +------+------------------+
- +------+------------------+
- +------+------------------+
- | 1 | 0.90576975597606 |
- | 2 | 0.37307905813035 |
- | 3 | 0.14808605345719 |
- +------+------------------+
- +------+------------------+
- +------+------------------+
- | 1 | 0.35877890638893 |
- | 2 | 0.28941420772058 |
- | 3 | 0.37073435016976 |
- +------+------------------+
- +------+------------------+
- +------+------------------+
- | 1 | 0.90576975597606 |
- | 2 | 0.37307905813035 |
- | 3 | 0.14808605345719 |
- +------+------------------+
RAND()
in aWHERE
clause is evaluated for every row (when selecting from one table) or combination of rows (when selecting from a multiple-table join). Thus, for optimizer purposes,RAND()
is not a constant value and cannot be used for index optimizations. For more information, see Section 8.2.1.18, “Function Call Optimization”.Use of a column with
RAND()
values in anORDER BY
orGROUP BY
clause may yield unexpected results because for either clause aRAND()
expression can be evaluated multiple times for the same row, each time returning a different result. If the goal is to retrieve rows in random order, you can use a statement like this:To select a random sample from a set of rows, combine
ORDER BY RAND()
withLIMIT
:RAND()
is not meant to be a perfect random generator. It is a fast way to generate random numbers on demand that is portable between platforms for the same MySQL version.This function is unsafe for statement-based replication. A warning is logged if you use this function when
binlog_format
is set toSTATEMENT
. (Bug #49222)Rounds the argument
X
toD
decimal places. The rounding algorithm depends on the data type ofX
.D
defaults to 0 if not specified.D
can be negative to causeD
digits left of the decimal point of the valueX
to become zero.- -> -1
- -> -2
- -> 2
- -> 1.3
- -> 1
- -> 20
The return value has the same type as the first argument (assuming that it is integer, double, or decimal). This means that for an integer argument, the result is an integer (no decimal places):
- +------------------+--------------+
- +------------------+--------------+
- | 150.00 | 150 |
- +------------------+--------------+
ROUND()
uses the following rules depending on the type of the first argument:For exact-value numbers,
ROUND()
uses the “round half away from zero” or “round toward nearest” rule: A value with a fractional part of .5 or greater is rounded up to the next integer if positive or down to the next integer if negative. (In other words, it is rounded away from zero.) A value with a fractional part less than .5 is rounded down to the next integer if positive or up to the next integer if negative.For approximate-value numbers, the result depends on the C library. On many systems, this means that
ROUND()
uses the “round to nearest even” rule: A value with a fractional part exactly halfway between two integers is rounded to the nearest even integer.
The following example shows how rounding differs for exact and approximate values:
- +------------+--------------+
- +------------+--------------+
- | 3 | 2 |
- +------------+--------------+
For more information, see Section 12.24, “Precision Math”.
Returns the sign of the argument as
-1
,0
, or1
, depending on whetherX
is negative, zero, or positive.Returns the sine of
X
, whereX
is given in radians.Returns the square root of a nonnegative number
X
.Returns the tangent of
X
, whereX
is given in radians.Returns the number
X
, truncated toD
decimal places. IfD
is0
, the result has no decimal point or fractional part.D
can be negative to causeD
digits left of the decimal point of the valueX
to become zero.- -> 1.2
- -> 1.9
- -> 1
- -> -1.9
- -> 100
- -> 1028
All numbers are rounded toward zero.
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-mathematical-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.