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.

  • ABS(X)

    Returns the absolute value of X.

    1. mysql> SELECT ABS(2);
    2.         -> 2
    3. mysql> SELECT ABS(-32);
    4.         -> 32

    This function is safe to use with BIGINT values.

  • ACOS(X)

    Returns the arc cosine of X, that is, the value whose cosine is X. Returns NULL if X is not in the range -1 to 1.

    1. mysql> SELECT ACOS(1);
    2.         -> 0
    3. mysql> SELECT ACOS(1.0001);
    4.         -> NULL
    5. mysql> SELECT ACOS(0);
    6.         -> 1.5707963267949
  • ASIN(X)

    Returns the arc sine of X, that is, the value whose sine is X. Returns NULL if X is not in the range -1 to 1.

    1. mysql> SELECT ASIN(0.2);
    2.         -> 0.20135792079033
    3. mysql> SELECT ASIN('foo');
    4.  
    5. +-------------+
    6. | ASIN('foo') |
    7. +-------------+
    8. |           0 |
    9. +-------------+
    10. 1 row in set, 1 warning (0.00 sec)
    11.  
    12. mysql> SHOW WARNINGS;
    13. +---------+------+-----------------------------------------+
    14. | Level   | Code | Message                                 |
    15. +---------+------+-----------------------------------------+
    16. | Warning | 1292 | Truncated incorrect DOUBLE value: 'foo' |
    17. +---------+------+-----------------------------------------+
  • ATAN(X)

    Returns the arc tangent of X, that is, the value whose tangent is X.

    1. mysql> SELECT ATAN(2);
    2.         -> 1.1071487177941
    3. mysql> SELECT ATAN(-2);
    4.         -> -1.1071487177941
  • ATAN(Y,X), ATAN2(Y,X)

    Returns the arc tangent of the two variables X and Y. It is similar to calculating the arc tangent of Y / X, except that the signs of both arguments are used to determine the quadrant of the result.

    1. mysql> SELECT ATAN(-2,2);
    2.         -> -0.78539816339745
    3. mysql> SELECT ATAN2(PI(),0);
    4.         -> 1.5707963267949
  • CEIL(X)

    CEIL() is a synonym for CEILING().

  • CEILING(X)

    Returns the smallest integer value not less than X.

    1. mysql> SELECT CEILING(1.23);
    2.         -> 2
    3. mysql> SELECT CEILING(-1.23);
    4.         -> -1

    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.

  • CONV(N,from_base,to_base)

    Converts numbers between different number bases. Returns a string representation of the number N, converted from base from_base to base to_base. Returns NULL if any argument is NULL. The argument N is interpreted as an integer, but may be specified as an integer or a string. The minimum base is 2 and the maximum base is 36. If from_base is a negative number, N is regarded as a signed number. Otherwise, N is treated as unsigned. CONV() works with 64-bit precision.

    1. mysql> SELECT CONV('a',16,2);
    2.         -> '1010'
    3. mysql> SELECT CONV('6E',18,8);
    4.         -> '172'
    5. mysql> SELECT CONV(-17,10,-18);
    6.         -> '-H'
    7. mysql> SELECT CONV(10+'10'+'10'+X'0a',10,10);
    8.         -> '40'
  • COS(X)

    Returns the cosine of X, where X is given in radians.

    1. mysql> SELECT COS(PI());
    2.         -> -1
  • COT(X)

    Returns the cotangent of X.

    1. mysql> SELECT COT(12);
    2.         -> -1.5726734063977
    3. mysql> SELECT COT(0);
    4.         -> out-of-range error
  • CRC32(expr)

    Computes a cyclic redundancy check value and returns a 32-bit unsigned value. The result is NULL if the argument is NULL. The argument is expected to be a string and (if possible) is treated as one if it is not.

    1. mysql> SELECT CRC32('MySQL');
    2.         -> 3259397556
    3. mysql> SELECT CRC32('mysql');
    4.         -> 2501908538
  • DEGREES(X)

    Returns the argument X, converted from radians to degrees.

    1. mysql> SELECT DEGREES(PI());
    2.         -> 180
    3. mysql> SELECT DEGREES(PI() / 2);
    4.         -> 90
  • EXP(X)

    Returns the value of e (the base of natural logarithms) raised to the power of X. The inverse of this function is LOG() (using a single argument only) or LN().

    1. mysql> SELECT EXP(2);
    2.         -> 7.3890560989307
    3. mysql> SELECT EXP(-2);
    4.         -> 0.13533528323661
    5. mysql> SELECT EXP(0);
    6.         -> 1
  • FLOOR(X)

    Returns the largest integer value not greater than X.

    1. mysql> SELECT FLOOR(1.23), FLOOR(-1.23);
    2.         -> 1, -2

    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.

  • FORMAT(X,D)

    Formats the number X to a format like '#,###,###.##', rounded to D decimal places, and returns the result as a string. For details, see Section 12.5, “String Functions and Operators”.

  • HEX(N_or_S)

    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 and Operators”, for details.

  • LN(X)

    Returns the natural logarithm of X; that is, the base-e logarithm of X. If X is less than or equal to 0.0E0, the function returns NULL and a warning Invalid argument for logarithm is reported.

    1. mysql> SELECT LN(2);
    2.         -> 0.69314718055995
    3. mysql> SELECT LN(-2);
    4.         -> NULL

    This function is synonymous with LOG(X). The inverse of this function is the EXP() function.

  • LOG(X), LOG(B,X)

    If called with one parameter, this function returns the natural logarithm of X. If X is less than or equal to 0.0E0, the function returns NULL and a warning Invalid argument for logarithm is reported.

    The inverse of this function (when called with a single argument) is the EXP() function.

    1. mysql> SELECT LOG(2);
    2.         -> 0.69314718055995
    3. mysql> SELECT LOG(-2);
    4.         -> NULL

    If called with two parameters, this function returns the logarithm of X to the base B. If X is less than or equal to 0, or if B is less than or equal to 1, then NULL is returned.

    1. mysql> SELECT LOG(2,65536);
    2.         -> 16
    3. mysql> SELECT LOG(10,100);
    4.         -> 2
    5. mysql> SELECT LOG(1,100);
    6.         -> NULL

    LOG(B,X) is equivalent to LOG(X) / LOG(B).

  • LOG2(X)

    Returns the base-2 logarithm of X. If X is less than or equal to 0.0E0, the function returns NULL and a warning Invalid argument for logarithm is reported.

    1. mysql> SELECT LOG2(65536);
    2.         -> 16
    3. mysql> SELECT LOG2(-100);
    4.         -> NULL

    LOG2() is useful for finding out how many bits a number requires for storage. This function is equivalent to the expression LOG(X) / LOG(2).

  • LOG10(X)

    Returns the base-10 logarithm of X. If X is less than or equal to 0.0E0, the function returns NULL and a warning Invalid argument for logarithm is reported.

    1. mysql> SELECT LOG10(2);
    2.         -> 0.30102999566398
    3. mysql> SELECT LOG10(100);
    4.         -> 2
    5. mysql> SELECT LOG10(-100);
    6.         -> NULL

    LOG10(X) is equivalent to LOG(10,X).

  • MOD(N,M), N % M, N MOD M

    Modulo operation. Returns the remainder of N divided by M.

    1. mysql> SELECT MOD(234, 10);
    2.         -> 4
    3. mysql> SELECT 253 % 7;
    4.         -> 1
    5. mysql> SELECT MOD(29,9);
    6.         -> 2
    7. mysql> SELECT 29 MOD 9;
    8.         -> 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:

    1. mysql> SELECT MOD(34.5,3);
    2.         -> 1.5

    MOD(N,0) returns NULL.

  • PI()

    Returns the value of π (pi). The default number of decimal places displayed is seven, but MySQL uses the full double-precision value internally.

    1. mysql> SELECT PI();
    2.         -> 3.141593
    3. mysql> SELECT PI()+0.000000000000000000;
    4.         -> 3.141592653589793116
  • POW(X,Y)

    Returns the value of X raised to the power of Y.

    1. mysql> SELECT POW(2,2);
    2.         -> 4
    3. mysql> SELECT POW(2,-2);
    4.         -> 0.25
  • POWER(X,Y)

    This is a synonym for POW().

  • RADIANS(X)

    Returns the argument X, converted from degrees to radians. (Note that π radians equals 180 degrees.)

    1. mysql> SELECT RADIANS(90);
    2.         -> 1.5707963267949
  • RAND([N])

    Returns a random floating-point value v in the range 0 <= v < 1.0. To obtain a random integer R in the range i <= R < j, use the expression FLOOR(i + RAND() * (ji)). For example, to obtain a random integer in the range the range 7 <= R < 12, use the following statement:

    1. SELECT FLOOR(7 + (RAND() * 5));

    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(N) returns the same value each time, and thus produces a repeatable sequence of column values. In the following example, the sequence of values produced by RAND(3) is the same both places it occurs.

    1. mysql> CREATE TABLE t (i INT);
    2. Query OK, 0 rows affected (0.42 sec)
    3.  
    4. mysql> INSERT INTO t VALUES(1),(2),(3);
    5. Query OK, 3 rows affected (0.00 sec)
    6. Records: 3  Duplicates: 0  Warnings: 0
    7.  
    8. mysql> SELECT i, RAND() FROM t;
    9. +------+------------------+
    10. | i    | RAND()           |
    11. +------+------------------+
    12. |    1 | 0.61914388706828 |
    13. |    2 | 0.93845168309142 |
    14. |    3 | 0.83482678498591 |
    15. +------+------------------+
    16. 3 rows in set (0.00 sec)
    17.  
    18. mysql> SELECT i, RAND(3) FROM t;
    19. +------+------------------+
    20. | i    | RAND(3)          |
    21. +------+------------------+
    22. |    1 | 0.90576975597606 |
    23. |    2 | 0.37307905813035 |
    24. |    3 | 0.14808605345719 |
    25. +------+------------------+
    26. 3 rows in set (0.00 sec)
    27.  
    28. mysql> SELECT i, RAND() FROM t;
    29. +------+------------------+
    30. | i    | RAND()           |
    31. +------+------------------+
    32. |    1 | 0.35877890638893 |
    33. |    2 | 0.28941420772058 |
    34. |    3 | 0.37073435016976 |
    35. +------+------------------+
    36. 3 rows in set (0.00 sec)
    37.  
    38. mysql> SELECT i, RAND(3) FROM t;
    39. +------+------------------+
    40. | i    | RAND(3)          |
    41. +------+------------------+
    42. |    1 | 0.90576975597606 |
    43. |    2 | 0.37307905813035 |
    44. |    3 | 0.14808605345719 |
    45. +------+------------------+
    46. 3 rows in set (0.01 sec)

    RAND() in a WHERE 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.19, “Function Call Optimization”.

    Use of a column with RAND() values in an ORDER BY or GROUP BY clause may yield unexpected results because for either clause a RAND() 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:

    1. SELECT * FROM tbl_name ORDER BY RAND();

    To select a random sample from a set of rows, combine ORDER BY RAND() with LIMIT:

    1. SELECT * FROM table1, table2 WHERE a=b AND c<d ORDER BY RAND() LIMIT 1000;

    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 to STATEMENT.

  • ROUND(X), ROUND(X,D)

    Rounds the argument X to D decimal places. The rounding algorithm depends on the data type of X. D defaults to 0 if not specified. D can be negative to cause D digits left of the decimal point of the value X to become zero.

    1. mysql> SELECT ROUND(-1.23);
    2.         -> -1
    3. mysql> SELECT ROUND(-1.58);
    4.         -> -2
    5. mysql> SELECT ROUND(1.58);
    6.         -> 2
    7. mysql> SELECT ROUND(1.298, 1);
    8.         -> 1.3
    9. mysql> SELECT ROUND(1.298, 0);
    10.         -> 1
    11. mysql> SELECT ROUND(23.298, -1);
    12.         -> 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):

    1. mysql> SELECT ROUND(150.000,2), ROUND(150,2);
    2. +------------------+--------------+
    3. | ROUND(150.000,2) | ROUND(150,2) |
    4. +------------------+--------------+
    5. |           150.00 |          150 |
    6. +------------------+--------------+

    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:

    1. mysql> SELECT ROUND(2.5), ROUND(25E-1);
    2. +------------+--------------+
    3. | ROUND(2.5) | ROUND(25E-1) |
    4. +------------+--------------+
    5. | 3          |            2 |
    6. +------------+--------------+

    For more information, see Section 12.25, “Precision Math”.

  • SIGN(X)

    Returns the sign of the argument as -1, 0, or 1, depending on whether X is negative, zero, or positive.

    1. mysql> SELECT SIGN(-32);
    2.         -> -1
    3. mysql> SELECT SIGN(0);
    4.         -> 0
    5. mysql> SELECT SIGN(234);
    6.         -> 1
  • SIN(X)

    Returns the sine of X, where X is given in radians.

    1. mysql> SELECT SIN(PI());
    2.         -> 1.2246063538224e-16
    3. mysql> SELECT ROUND(SIN(PI()));
    4.         -> 0
  • SQRT(X)

    Returns the square root of a nonnegative number X.

    1. mysql> SELECT SQRT(4);
    2.         -> 2
    3. mysql> SELECT SQRT(20);
    4.         -> 4.4721359549996
    5. mysql> SELECT SQRT(-16);
    6.         -> NULL
  • TAN(X)

    Returns the tangent of X, where X is given in radians.

    1. mysql> SELECT TAN(PI());
    2.         -> -1.2246063538224e-16
    3. mysql> SELECT TAN(PI()+1);
    4.         -> 1.5574077246549
  • TRUNCATE(X,D)

    Returns the number X, truncated to D decimal places. If D is 0, the result has no decimal point or fractional part. D can be negative to cause D digits left of the decimal point of the value X to become zero.

    1. mysql> SELECT TRUNCATE(1.223,1);
    2.         -> 1.2
    3. mysql> SELECT TRUNCATE(1.999,1);
    4.         -> 1.9
    5. mysql> SELECT TRUNCATE(1.999,0);
    6.         -> 1
    7. mysql> SELECT TRUNCATE(-1.999,1);
    8.         -> -1.9
    9. mysql> SELECT TRUNCATE(122,-2);
    10.        -> 100
    11. mysql> SELECT TRUNCATE(10.28*100,0);
    12.        -> 1028

    All numbers are rounded toward zero.


Find a PHP function

Document created the 26/06/2006, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/mysql-rf-mathematical-functions.html

The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.

References

  1. View the html document Language of the document:en Manuel MySQL : https://dev.mysql.com/

These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.

Contents Haut