MySQL Tutorial MySQL Advanced MySQL Database Account Management MySQL References

MySQL LOG() Function



The MySQL LOG() function returns the natural logarithm or logarithm to the specified base of a given number. In special cases it returns the following:

  • If the number is less than or equal to 0, then NULL is returned.
  • If the base is less than or equal to 0, then NULL is returned.
  • If the base is equal to 1, then NULL is returned.

Syntax

/* natural logarithm of number */
LOG(number)

/* logarithm of number to the specified base */
LOG(base, number)

Parameters

base Specify the number. Must be greater than 0 and not equal to 1.
number Required. Specify the number. Must be greater than 0.

Return Value

Returns the natural logarithm or logarithm to the specified base of a given number.

Example 1:

The example below shows the usage of LOG() function.

mysql> SELECT LOG(1);
Result: 0

mysql> SELECT LOG(1.5);
Result: 0.4054651081081644

mysql> SELECT LOG(5);
Result: 1.6094379124341003

mysql> SELECT LOG(0);
Result: NULL

mysql> SELECT LOG(3, 4);
Result: 1.2618595071429148

mysql> SELECT LOG(0.7, 0.8);
Result: 0.6256216061886871

mysql> SELECT LOG(0.5, 10);
Result: -3.3219280948873626

mysql> SELECT LOG(5, 25);
Result: 2

Example 2:

Consider a database table called Sample with the following records:

Datax
Data 10.5
Data 21
Data 35
Data 410
Data 550

The statement given below can be used to calculate the base-10 logarithm of column x.

SELECT *, LOG(10, x) AS LOG_Value FROM Sample;

This will produce the result as shown below:

DataxLOG_Value
Data 10.5-0.30102999566398114
Data 210
Data 350.6989700043360187
Data 4101
Data 5501.6989700043360185

Example 3:

Consider a database table called Sample with the following records:

Dataxy
Data 10.52
Data 212
Data 352
Data 41010
Data 55010

To calculate the logarithm of records of column x with base as records of column y, the following query can be used:

SELECT *, LOG(y, x) AS LOG_Value FROM Sample;

This will produce the result as shown below:

DataxyLOG_Value
Data 10.52-1
Data 2120
Data 3522.321928094887362
Data 410101
Data 550101.6989700043360185

❮ MySQL Functions