MySQL - LOG() Function



The MySQL LOG() function accepts a numerical value as a parameter and returns the natural logarithm of it.

In other words, this function returns the base-e logarithm of the given value. You can also pass another parameter say B, to this function. If you do so, this function returns the logarithm of the given value to the base B.

Syntax

Following is the syntax of MySQL LOG() function −

LOG(B, X)

Parameters

This function takes a numeric value and an optional base as a parameter.

Return Value

This function returns the logarithm of the given value with the specified base.

Example

The following query uses MySQL LOG() function to calculate the natural logarithm (base e) of the value 9 −

SELECT LOG(9) As Result;

Output

This will produce the following result −

Result
2.1972245773362196

Example

Following is another example of this function where we are calculating the natural logarithm (base e) of the decimal value 26545.847 −

SELECT LOG(26545.847) As Result;

Output

The output is displayed as below −

Result
10.186628592589338

Example

If the value passed to the function is less than or equal to 0.0E0, it returns NULL.

Here, we are passing 0 as a parameter to this function −

SELECT LOG(0) As Result;

This will produce the following result −

Result
NULL

Now, we are passing -3 as a parameter to this function −

SELECT LOG(-3) As Result;

Following is the output −

Result
NULL

Example

The MySQL LOG() function is inverse of the EXP() function. Here, we are passing 90 as a parameter to the EXP() function −

SELECT EXP(90) As Result;

The output will be displayed as below −

Result
1.2204032943178408e39

Now, we can use the output of above function as parameter to LOG() function to retrieve the original value −

SELECT LOG(1.2204032943178408e39) As Result;

Following is the output −

Result
1.2204032943178408e39

Example

In the example below, we are using the LOG() function with two parameters. It calculates the logarithm of 44747 with a base of 2 −

SELECT LOG(2, 44747) As Result;

Output

Following is the output −

Result
15.449503341698307

Example

If the base value is less than or equal to 1 this function returns NULL

SELECT LOG(1, 567986) As Result;

Output

The output for the query above is produced as given below −

Result
NULL
Advertisements