MySQL :: MySQL 8.4 Reference Manual :: 14.6.2 Mathematical Functions (original) (raw)

14.6.2 Mathematical Functions

All mathematical functions return NULL in the event of an error.

mysql> SELECT ABS(2);  
        -> 2  
mysql> SELECT ABS(-32);  
        -> 32  

This function is safe to use withBIGINT values.

mysql> SELECT ACOS(1);  
        -> 0  
mysql> SELECT ACOS(1.0001);  
        -> NULL  
mysql> SELECT ACOS(0);  
        -> 1.5707963267949  
mysql> SELECT ASIN(0.2);  
        -> 0.20135792079033  
mysql> SELECT ASIN('foo');  
+-------------+  
| ASIN('foo') |  
+-------------+  
|           0 |  
+-------------+  
1 row in set, 1 warning (0.00 sec)  
mysql> SHOW WARNINGS;  
+---------+------+-----------------------------------------+  
| Level   | Code | Message                                 |  
+---------+------+-----------------------------------------+  
| Warning | 1292 | Truncated incorrect DOUBLE value: 'foo' |  
+---------+------+-----------------------------------------+  
mysql> SELECT ATAN(2);  
        -> 1.1071487177941  
mysql> SELECT ATAN(-2);  
        -> -1.1071487177941  
mysql> SELECT ATAN(-2,2);  
        -> -0.78539816339745  
mysql> SELECT ATAN2(PI(),0);  
        -> 1.5707963267949  
mysql> SELECT CEILING(1.23);  
        -> 2  
mysql> SELECT CEILING(-1.23);  
        -> -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.

mysql> SELECT CONV('a',16,2);  
        -> '1010'  
mysql> SELECT CONV('6E',18,8);  
        -> '172'  
mysql> SELECT CONV(-17,10,-18);  
        -> '-H'  
mysql> SELECT CONV(10+'10'+'10'+X'0a',10,10);  
        -> '40'  
mysql> SELECT COS(PI());  
        -> -1  
mysql> SELECT COT(12);  
        -> -1.5726734063977  
mysql> SELECT COT(0);  
        -> out-of-range error  
mysql> SELECT CRC32('MySQL');  
        -> 3259397556  
mysql> SELECT CRC32('mysql');  
        -> 2501908538  
mysql> SELECT DEGREES(PI());  
        -> 180  
mysql> SELECT DEGREES(PI() / 2);  
        -> 90  
mysql> SELECT EXP(2);  
        -> 7.3890560989307  
mysql> SELECT EXP(-2);  
        -> 0.13533528323661  
mysql> SELECT EXP(0);  
        -> 1  
mysql> SELECT FLOOR(1.23), FLOOR(-1.23);  
        -> 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.

mysql> SELECT LN(2);  
        -> 0.69314718055995  
mysql> SELECT LN(-2);  
        -> NULL  

This function is synonymous withLOG(X). The inverse of this function is theEXP() function.

mysql> SELECT LOG(2);  
        -> 0.69314718055995  
mysql> SELECT LOG(-2);  
        -> NULL  

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

mysql> SELECT LOG(2,65536);  
        -> 16  
mysql> SELECT LOG(10,100);  
        -> 2  
mysql> SELECT LOG(1,100);  
        -> NULL  

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

mysql> SELECT LOG2(65536);  
        -> 16  
mysql> SELECT LOG2(-100);  
        -> NULL  

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

mysql> SELECT LOG10(2);  
        -> 0.30102999566398  
mysql> SELECT LOG10(100);  
        -> 2  
mysql> SELECT LOG10(-100);  
        -> NULL  

LOG10(X) is equivalent toLOG(10,X).

mysql> SELECT MOD(234, 10);  
        -> 4  
mysql> SELECT 253 % 7;  
        -> 1  
mysql> SELECT MOD(29,9);  
        -> 2  
mysql> SELECT 29 MOD 9;  
        -> 2  

This function is safe to use withBIGINT values.
MOD() also works on values that have a fractional part and returns the exact remainder after division:

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

MOD(N,0) returns NULL.

mysql> SELECT PI();  
        -> 3.141593  
mysql> SELECT PI()+0.000000000000000000;  
        -> 3.141592653589793000  
mysql> SELECT POW(2,2);  
        -> 4  
mysql> SELECT POW(2,-2);  
        -> 0.25  
mysql> SELECT RADIANS(90);  
        -> 1.5707963267949  
SELECT FLOOR(7 + (RAND() * 5));  

If an integer argument N is specified, it is used as the seed value:

mysql> CREATE TABLE t (i INT);  
Query OK, 0 rows affected (0.42 sec)  
mysql> INSERT INTO t VALUES(1),(2),(3);  
Query OK, 3 rows affected (0.00 sec)  
Records: 3  Duplicates: 0  Warnings: 0  
mysql> SELECT i, RAND() FROM t;  
+------+------------------+  
| i    | RAND()           |  
+------+------------------+  
|    1 | 0.61914388706828 |  
|    2 | 0.93845168309142 |  
|    3 | 0.83482678498591 |  
+------+------------------+  
3 rows in set (0.00 sec)  
mysql> SELECT i, RAND(3) FROM t;  
+------+------------------+  
| i    | RAND(3)          |  
+------+------------------+  
|    1 | 0.90576975597606 |  
|    2 | 0.37307905813035 |  
|    3 | 0.14808605345719 |  
+------+------------------+  
3 rows in set (0.00 sec)  
mysql> SELECT i, RAND() FROM t;  
+------+------------------+  
| i    | RAND()           |  
+------+------------------+  
|    1 | 0.35877890638893 |  
|    2 | 0.28941420772058 |  
|    3 | 0.37073435016976 |  
+------+------------------+  
3 rows in set (0.00 sec)  
mysql> SELECT i, RAND(3) FROM t;  
+------+------------------+  
| i    | RAND(3)          |  
+------+------------------+  
|    1 | 0.90576975597606 |  
|    2 | 0.37307905813035 |  
|    3 | 0.14808605345719 |  
+------+------------------+  
3 rows in set (0.01 sec)  

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, seeSection 10.2.1.20, “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:

SELECT * FROM tbl_name ORDER BY RAND();  

To select a random sample from a set of rows, combineORDER BY RAND() withLIMIT:

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 whenbinlog_format is set toSTATEMENT.

mysql> SELECT ROUND(-1.23);  
        -> -1  
mysql> SELECT ROUND(-1.58);  
        -> -2  
mysql> SELECT ROUND(1.58);  
        -> 2  
mysql> SELECT ROUND(1.298, 1);  
        -> 1.3  
mysql> SELECT ROUND(1.298, 0);  
        -> 1  
mysql> SELECT ROUND(23.298, -1);  
        -> 20  
mysql> SELECT ROUND(.12345678901234567890123456789012345, 35);  
        -> 0.123456789012345678901234567890  

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):

mysql> SELECT ROUND(150.000,2), ROUND(150,2);  
+------------------+--------------+  
| ROUND(150.000,2) | ROUND(150,2) |  
+------------------+--------------+  
|           150.00 |          150 |  
+------------------+--------------+  

ROUND() uses the following rules depending on the type of the first argument:

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

For more information, see Section 14.24, “Precision Math”.
The data type returned by ROUND() (andTRUNCATE()) is determined according to the rules listed here:

mysql> SELECT SIGN(-32);  
        -> -1  
mysql> SELECT SIGN(0);  
        -> 0  
mysql> SELECT SIGN(234);  
        -> 1  
mysql> SELECT SIN(PI());  
        -> 1.2246063538224e-16  
mysql> SELECT ROUND(SIN(PI()));  
        -> 0  
mysql> SELECT SQRT(4);  
        -> 2  
mysql> SELECT SQRT(20);  
        -> 4.4721359549996  
mysql> SELECT SQRT(-16);  
        -> NULL  
mysql> SELECT TAN(PI());  
        -> -1.2246063538224e-16  
mysql> SELECT TAN(PI()+1);  
        -> 1.5574077246549  
mysql> SELECT TRUNCATE(1.223,1);  
        -> 1.2  
mysql> SELECT TRUNCATE(1.999,1);  
        -> 1.9  
mysql> SELECT TRUNCATE(1.999,0);  
        -> 1  
mysql> SELECT TRUNCATE(-1.999,1);  
        -> -1.9  
mysql> SELECT TRUNCATE(122,-2);  
       -> 100  
mysql> SELECT TRUNCATE(10.28*100,0);  
       -> 1028  

All numbers are rounded toward zero.
The data type returned by TRUNCATE() follows the same rules that determine the return type of theROUND() function; for details, see the description for ROUND().