Detailed explanation and practical use of MYSQL numeric functions

MySQL provides many numeric functions for performing various calculations and processing on numbers. The following are some commonly used MySQL numeric functions.

There is a lot of content, it is recommended to save it for future reference.

Table of contents

1. SIGN takes the symbol of the number

2. ABS takes the absolute value

3. COS takes the cosine value

4. ACOS takes the inverse cosine value

5. SIN takes the sine value

6. ASIN takes the inverse sine value

7. TAN takes tangent value

8. ATAN takes the arctangent value

9. ATAN2 takes the arc tangent value

10. COT takes the cotangent value

11. CEIL rounded up

12. CEILING Rounding up

13. FLOOR round down

14. ROUND rounding off

15. TRUNCATE intercepts decimal places

16. CONV base to another base

17. DIV gets division value

18. EXP e raised to the power of the specified value

19. GREATEST takes the maximum value of the list

20. LEAST takes the minimum value of the list

21. LN takes the natural logarithm of the specified number

22. LOG: The logarithm of the specified base of the specified number.

23. LOG10 The base 10 logarithm of the specified number

24. LOG2 is the base 2 logarithm of the specified number.

25. MOD takes the remainder of two numbers

26. PI value

27. POW calculates the y power of x

28. POWER calculates the y power of x

29. SQRT takes the square root

 30. DEGREES Convert radians to degrees

31. RADIANS Convert degree value to radian value

32. RAND takes a random number from 0 to 1


1. SIGN takes the symbol of the number

MySQL SIGN() The function returns the sign of the specified number. When the parameters are positive, negative and zero, 1-1 and 0 are returned respectively.

grammar:

SIGN(number)

Parameter Description:

  • number: Required. a number.

return value:

  •  Reply 1-1,0 和 NULL 中的一个:
    • Result number Dayu 0,SIGN() function general return 1.
    • Result number 等于 0,SIGN() function general return 0.
    • result number 小于 0SIGN() function general return -1.
    • result number 为 NULL,SIGN() function general return NULL.

Example:

SELECT
    SIGN(123),	                        -- 1
    SIGN(123.123),	                    -- 1
    SIGN(-123),	                        -- -1
    SIGN(-123.123),	                    -- -1
    SIGN(0),	                        -- 0
    SIGN(NULL)	                        -- NULL

2. ABS takes the absolute value

MySQL ABS() The function returns the absolute value of the specified number.

grammar:

ABS(number)

Parameter Description:

  • numberRequired. A number used to calculate the absolute value.

return value:

  • Returns the absolute (positive) value of a number.
  • If the parameter number is a string, ABS() will try to convert it to a number according to the following rules:
    • If it starts with a number, convert the starting number into a number.
    • If cannot be converted into a number, it will be processed as 0 .
  • Reply NULL情况:number 为 NULL。

Example:

SELECT
    ABS(100),		    -- 100
    ABS(-100),          -- 100
    ABS('100'),         -- 100
    ABS('-100'),        -- 100
    ABS('-100A')        -- 100
    ABS('A100'),        -- 0
    ABS(NULL)           -- null

3. COS takes the cosine value

MySQL COS() The function returns the cosine of the specified value.

grammar:

COS(number)

Parameter Description:

  • number: Required. A numeric value used to calculate the cosine value.

return value:

  • Cosine of the specified number.
  • Reply NULL情况:number 为 NULL。

Example:

SELECT
    COS(2.5),	     -- -0.8011436155469337
    COS(0.2),	     -- 0.9800665778412416
    COS(-0.5),       -- 0.8775825618903728
    COS(-0.2),	     -- 0.9800665778412416
    COS(0),          -- 1
    COS(PI()),       -- -1
    COS(NULL)        -- NULL

4. ACOS takes the inverse cosine value

MySQL ACOS() The function returns the arc cosine of the specified value.

grammar:

ACOS(number)

Parameter Description:

  • number: Required. A number used to calculate the arc cosine.

return value:

  • Returns the arc cosine of the specified number.
  • return NULL情况:
    • number absence  -1 arrival 1 while.
    • number for NULL。

Example:

SELECT
    ACOS(0.5),	    -- 1.0471975511965976
    ACOS(0.2),	    -- 1.3694384060045657
    ACOS(-0.5),	    -- 2.0943951023931957
    ACOS(-0.2),	    -- 1.7721542475852274
    ACOS(1),		-- 0
    ACOS(0),		-- 1.5707963267948966
    ACOS(-1),		-- 3.141592653589793
    ACOS(2),		-- NULL
    ACOS(-2),		-- NULL
    ACOS(NULL)	    -- NULL

5. SIN takes the sine value

MySQL SIN() The function returns the sine of the specified value.

grammar:

SIN(number)

Parameter Description:

  • number: Required. A numeric value used to calculate the sine value.

return value:

  • The sine of the specified number.
  • Reply NULL情况:number 为 NULL。

Example:

SELECT
    SIN(2.5),	    -- 0.5984721441039564
    SIN(0.2),	    -- 0.19866933079506122
    SIN(-0.5),      -- -0.479425538604203
    SIN(0),         -- 0
    SIN(PI()),      -- 1.2246467991473532e-16
    SIN(NULL)       -- NULL

6. ASIN takes the inverse sine value

MySQL ASIN() The function returns the arcsine of the specified value.

grammar:

ASIN(number)

Parameter Description:

  • number: Required. A numeric value used to calculate the arcsine.

return value:

  • The arcsine of the specified number.
  • return NULL情况:
    • number absence  -1 arrival 1 while.
    • number for NULL。

Example:

SELECT
    ASIN(0.5),	    -- 0.5235987755982988
    ASIN(-0.5),	    -- -0.5235987755982988
    ASIN(1),		-- 1.5707963267948966
    ASIN(0),		-- 0
    ASIN(-1),	    -- -1.5707963267948966
    ASIN(2),	    -- NULL
    ASIN(NULL)	    -- NULL

7. TAN takes tangent value

MySQL TAN() The function returns the tangent of the specified value.

grammar:

TAN(number)

Parameter Description:

  • number: Required. A number used to calculate the tangent.

return value:

  • The tangent of a specified number.
  • Reply NULL情况:number 为 NULL。

Example:

SELECT
    TAN(2.5),		-- -0.7470222972386602
    TAN(0.2),		-- 0.20271003550867248
    TAN(-0.5),	    -- -0.5463024898437905
    TAN(0),		    -- 0
    TAN(PI()),	    -- -1.2246467991473532e-16
    TAN(NULL)		-- NULL

8. ATAN takes the arctangent value

MySQL ATAN() The function returns the arc tangent of the specified value.

grammar:

ATAN(number)
ATAN(x, y)

Parameter Description:

  • number: Required. A number used to calculate the arctangent.
  • x, y: required. A two-numeric value used to calculate the arctangent. The symbols x and y are used to determine the quadrant of the results.

return value:

  • The arctangent of the specified number.
  • Reply NULL情况:number 为 NULL。

Example:

SELECT
    ATAN(2.5),		-- 1.1902899496825317
    ATAN(0.2),		-- 0.19739555984988078
    ATAN(-0.5),	    -- -0.46364760900080615
    ATAN(-0.9, 2),	-- -0.4228539261329407
    ATAN(PI(), 2),	-- 1.0038848218538872
    ATAN(NULL)		-- NULL

9. ATAN2 takes the arc tangent value

MySQL ATAN2() The function returns the arc tangent of the specified value.

grammar:

ATAN2(x, y)

Parameter Description:

  • x, y: required. A two-numeric value used to calculate the arctangent. The symbols x and y are used to determine the quadrant of the results.

return value:

  • The arctangent of the specified number.
  • Return NULL情况:Any parameter is NULL。

Example:

SELECT
    ATAN2(2.5, 1),	    -- 1.1902899496825317
    ATAN2(0.75, 1),	    -- 0.6435011087932844
    ATAN2(-0.9, 2),	    -- -0.4228539261329407
    ATAN2(PI(), 2),	    -- 1.0038848218538872
    ATAN2(NULL)		    -- NULL

10. COT takes the cotangent value

MySQL COT() The function returns the cotangent value of the specified value.

grammar:

COT(number)

Parameter Description:

  • number: Required. A number used to calculate the cotangent value.

return value:

  • The cotangent of a specified number.
  • Reply NULL情况:number 为 NULL。

Example:

SELECT
    COT(2.5),	    -- -1.3386481283041516
    COT(0.2),	    -- 4.933154875586894
    COT(-0.5),	    -- -1.830487721712452
    COT(PI()),	    -- -8.165619676597685e15
    COT(NULL)		-- NULL

11. CEIL rounded up

MySQL CEIL() The function returns the smallest integer value greater than or equal to the specified number. The CEIL() function is equivalent to the CEILING() function.

grammar:

CEIL(number)

Parameter Description:

  • number: Required. a number.

return value:

  • The smallest integer value greater than or equal to the specified number.
  • Reply NULL情况:number 为 NULL。

Example:

SELECT
    CEIL(123.123),	    -- 124
    CEIL(123),	        -- 123
    CEIL(-123.123),	    -- -123
    CEIL(-123),	        -- -123
    CEIL(-100),	        -- -100
    CEIL(NULL)	        -- NULL

12. CEILING Rounding up

The MySQL CEILING() function returns the smallest integer value greater than or equal to the specified number. The CEILING() function is equivalent to the CEIL() function.

grammar:

CEILING(number)

Numerical explanation

  • number: Required. a number.

return value:

  • The smallest integer value greater than or equal to the specified number.
  • Reply NULL情况:number 为 NULL。

Example:

SELECT
    CEILING(123.789),	-- 124
    CEILING(123),	-- 123
    CEILING(-123.123),	-- -123
    CEILING(-123),	-- -123
    CEILING(NULL)-- NULL

13. FLOOR round down

MySQL FLOOR() The function returns the largest integer value less than or equal to the specified number.

grammar:

FLOOR(number)

Parameter Description:

  • number: Required. a number.

return value:

  • The largest integer value less than or equal to the specified number.
  • Reply NULL情况:number 为 NULL。

Example:

SELECT
    FLOOR(123.123),	    -- 123
    FLOOR(123),	        -- 123
    FLOOR(-123.123),	-- -124
    FLOOR(-123),	    -- -123
    FLOOR(NULL)	        -- NULL

14. ROUND rounding off

The MySQL ROUND() function rounds a number to a specified number of decimal places.

grammar:

ROUND(x, d)

Parameter Description:

  • x: Required. The number being processed.
  • d: Required. The number of decimal places to keep.

return value:

  • Rounds a number to a specified number of decimal places.
  • If d is greater than or equal to x the number of decimal places, the original number is returned.
  • If d is less than the number of decimal places of x , then round the number of decimal places of x to  Return after d bits.
  • If d is a negative number, the ROUND() function will replace the d digits of the integer part from the decimal point to 0.
  • returns NULL情况:任means one parameter is NULL。

Example:

SELECT
    ROUND(123.179, 1),	    -- 123.2
    ROUND(123.179, 4),	    -- 123.179
    ROUND(123.179, 0),	    -- 123
    ROUND(123.179, -2),	    -- 100
    ROUND(123.179, NULL)	-- NULL

15. TRUNCATE intercepts decimal places

MySQL TRUNCATE() function intercepts the number to the specified number of decimal places. TRUNCATE() The function only truncates the decimal by the specified number of digits without rounding.

grammar:

TRUNCATE(x, d)

Parameter Description:

  • x: Required. The number being processed.
  • d: Required. The number of decimal places to keep.

return value:

  • Numbers with specified decimal places preserved.
  • If d is greater than or equal to x the number of decimal places, the original number is returned.
  • If d is less than the number of decimal places of x , then truncate the number of decimal places of x to d bits.
  • If d is a negative number, the TRUNCATE() function will replace the d digits of the integer part from the decimal point to 0.
  • Return NULL情况:Any parameter is NULL。

Example:

SELECT
    TRUNCATE(123.179, 1),		-- 123.1
    TRUNCATE(123.179, 4),		-- 123.179
    TRUNCATE(123.179, 0),		-- 123
    TRUNCATE(123.179, -2),	    -- 100
    TRUNCATE(123.179, NULL)	    -- NULL

16. CONV base to another base

MySQL CONV() function converts numbers from one base to another, such as from base 10 to base 2.

grammar:

CONV(num, from_base, to_base)

Parameter Description:

  • num: Required. a number.
  • from_base: Required. The base currently used for numbers. From 2 to 36.
  • to_base: Required. The base to which the number is converted. From 2 to 36.

return value:

  • Convert numbers from one base to another.
  • Return NULL情况:Any parameter is NULL。

Example:

SELECT 
    CONV('A', 16, 10),		-- 10
    CONV('C', 16, 10),		-- 12
    CONV('E', 16, 10),		-- 14
    CONV('F', 16, NULL)		-- NULL

17. DIV gets division value

In MySQL, the DIV operator computes the division of two integers and returns an integer result.

grammar:

x DIV y

Parameter Description:

  • x: Required. dividend.
  • y: Required. divisor.

return value:

  • x DIV y returns the integer part of the result of dividing x by y .
  • If the parameter x or y is not an integer, they will be converted to DECIMAL type before calculation.

Example:

SELECT
    10 DIV 3,	    -- 3
    10 / 3,	        -- 3.3333
    FLOOR(10/3),	-- 3
    9.8 DIV 2.6,	-- 3
    9.8 / 2.6,	    -- 3.76923
    FLOOR(9.8/2.6)	-- 3

18. EXP e raised to the power of the specified value

MySQL EXP() The function returns the natural constant e raised to the power of the specified value. For example, EXP(2) returns the result  a>e2.

grammar:

EXP(number)

Parameter Description:

  • number: Required. Power value.

return value:

  • The natural constant e is raised to the power of the specified value.
  • Reply NULL情况:number 为 NULL。

Example:

SELECT
    EXP(0),		    -- 1
    EXP(2),		    -- 7.38905609893065
    EXP(-1),		-- 0.36787944117144233
    EXP(NULL)	    -- NULL

19. GREATEST takes the maximum value of the list

MySQL GREATEST() The function returns the maximum value in the parameter list.

grammar:

GREATEST(param1, param2, ..., paramN)

Parameter Description:

  • param1, param2, ..., paramN:Required. List of parameters to use for comparison. All parameters are included in the comparison. Parameters can be any data type, or expression.

return value:

  • The maximum value in the parameter list.
  • Return NULL情况:Any parameter is NULL。

Example:

SELECT
    GREATEST(2, 1, 5),		        -- 5
    GREATEST(2, 1, 5, '0'),	        -- 5
    GREATEST('a', 'b', 'c'),	    -- c
    GREATEST('Hello', 'World'),	    -- World
    GREATEST('a', 'b', NULL)		-- NULL

20. LEAST takes the minimum value of the list

MySQL LEAST() The function returns the minimum value in the parameter list.

grammar:

LEAST(param1, param2, ..., paramN)

Parameter Description:

  • param1, param2, ..., paramN: Required. List of parameters to use for comparison. All parameters are included in the comparison. Parameters can be any data type, or expression.

return value:

  • The minimum value in the argument list.
  • Return NULL情况:Any parameter is NULL。

Example:

SELECT
    LEAST(2, 1, 5),		        -- 1
    LEAST(2, 1, 5, '0'),	    -- 0
    LEAST('a', 'b', 'c'),		-- a
    LEAST('Hello', 'World'),	-- Hello
    LEAST('a', 'b', NULL)	    -- NULL

21. LN takes the natural logarithm of the specified number

MySQL LN() The function returns the natural logarithm of the specified number.

grammar:

LN(number)

Parameter Description:

  • number: required. A number used to calculate the natural logarithm. The value must be greater than 0.

return value:

  • The natural logarithm of the specified number.
  • return NULL情况:
    • number less than or equal to 0。
    • number for NULL。

Example:

SELECT
    LN(1),		-- 0
    LN(2),		-- 0.6931471805599453
    LN(0),      -- NULL
    LN(-1),		-- NULL
    LN(NULL)    -- NULL

22. LOG: The logarithm of the specified base of the specified number.

MySQL LOG() The function returns the logarithm of the specified base to the specified number.

grammar:

LOG(number)
LOG(base, number)

Parameter Description:

  • number: required. A number used to calculate logarithms. The value must be greater than 0.
  • base: Optional parameter. base. The value must be greater than 1.

return value:

  • The logarithm of the specified number to the specified base.
  • return NULL情况:
    • number less than or equal to 0。
    • base less than or equal to 1。
    • Any parameter is NULL。

Example:

SELECT
    LOG(1),		        -- 0
    LOG(EXP(1), 2),	    -- 0.6931471805599453
    LOG(10, 100),	    -- 2
    LOG(-1),		    -- NULL
    LOG(1, 10),	        -- NULL
    LOG(NULL)	        -- NULL

23. LOG10 The base 10 logarithm of the specified number

The MySQL LOG10() function returns the base 10 logarithm of the specified number. Equivalent to the LOG(10, number) function.

24. LOG2 is the base 2 logarithm of the specified number.

The MySQL LOG2() function returns the base 2 logarithm of the specified number. Equivalent to the LOG(2, number) function.

25. MOD takes the remainder of two numbers

MySQL MOD() The function returns the remainder of one number divided by another number.

grammar:

MOD(number1, number2)
number1 MOD number2
number1 % number2

Parameter Description:

  • number1: Required. dividend.
  • number2: Required. divisor.

return value:

  • The remainder after dividing one number by another number.
  • return NULL情况:
    • number2 equal to 0。
    • number1 for NULL。

Example:

SELECT
    MOD(100, 10),	-- 0
    100 MOD 7,	    -- 2
    100 % 7,	    -- 2
    100 % 10,	    -- 0
    MOD(0, 1),	    -- 0
    MOD(1, 0),	    -- NULL
    MOD(NULL, 1)	-- NULL

26. PI value

MySQL PI() The function returns the PI value with 6 decimal places.

grammar:

PI()

Example:

SELECT
    PI(),		            -- 3.141593
    PI() + 0.000000,	    -- 3.141593
    PI() * 1000000000000	-- 3141592653589.793000

Twenty-seven, POW calculation x ’s y power

MySQL POW(x, y) function calculates x to the y power and returns the result, that is: multiply y x's.

grammar:

POW(x, y)

Parameter Description:

  • x: Required. The base in calculations of powers.
  • y: Required. Exponents in power calculations.

return value:

  • x is raised to the power of y and the result is returned.
  • returns NULL情况: any parameter is NULL。

Example:

SELECT
    POW(2, 0),		    -- 1
    POW(2.5, 2),		-- 6.25
    POW(2, -4),		    -- 0.0625
    POW(2, NULL)	    -- NULL

28, POWER account x to y next

The MySQL POWER(x, y) function calculates x raised to the power of y and returns the result, which is: xy. The POWER() function is equivalent to the POW() function.

29. SQRT takes the square root

MySQL SQRT() The function returns the square root of a number.

grammar:

SQRT(number)

Parameter Description:

  • number: required. A number used to calculate logarithms. The value must be greater than 0.

return value:

  • The square root of parameter number .
  • returns NULL情况:number is a negative number or NULL。

Example:

SELECT
    SQRT(16),	-- 4
    SQRT(101),	-- 10.04987562112089
    SQRT(-100),	-- NULL
    SQRT(NULL)	-- NULL

 30. DEGREES Convert radians to degrees

MySQL DEGREES() The function converts the radian value into degrees and returns it.

grammar:

DEGREES(number)

Parameter Description:

  • number: Required. A number representing radians.

return value:

  • The degree corresponding to the specified radian value.
  • Reply NULL情况:number 为 NULL。

Example:

SELECT
    RADIANS(0),		    -- 0
    RADIANS(360),	    -- 6.283185307179586
    RADIANS(-360),	    -- -6.283185307179586
    RADIANS(NULL)		-- NULL
SELECT
    DEGREES(0.75),	    -- 42.97183463481174
    DEGREES(-0.75),	    -- -42.97183463481174
    DEGREES(0),	        -- 0
    DEGREES(PI() * 2),	-- 360
    DEGREES(PI() * -1),	-- -180
    DEGREES(NULL)	    -- NULL

31.RADIANS Convert degree value to radian value

MySQL RADIANS() The function converts the degree value into a radian value and returns it.

grammar:

RADIANS(number)

Parameter Description:

  • number: Required. A number representing degrees.

return value:

  • The degree corresponding to the specified radian value.
  • Reply NULL情况:number 为 NULL。

Example:

32. RAND takes a random number from 0 to 1

The MySQL RAND() function returns a random number between 0 (inclusive) and 1 (exclusive).

grammar:

RAND()
RAND(seed)

Parameter Description:

  • seed: Optional. Generate a seed for random numbers. If seed is provided, the RAND(seed) function will generate a repeatable sequence of random numbers.

return value:

  • A random number between 0 (inclusive) and 1 (exclusive).
  • If the parameter seed is provided, the MySQL RAND(seed) function will generate a repeatable sequence of random numbers.
  • If you want to return random numbers in other ranges (such as m and n), please use the following formula:
    • m Random number between and n : RAND() * (n - m) + m
    • m Random integer between and n : FLOOR(RAND() * (n - m + 1) + m)

Example:

SELECT
    RAND(),		-- 0.9892005063492809
    RAND(),		-- 0.2601393091121123
    RAND(),		-- 0.3330950572463638
    RAND()		-- 0.8850573896291268

Guess you like

Origin blog.csdn.net/imwucx/article/details/133913883