The LOG() function in MySQL is used to return the logarithm of a specified number, either with the natural logarithm (base e) or with a specified base.
Syntax
LOG(number)
or
LOG(base, number)
Definition and Usage
- The
LOG()function computes the natural logarithm (logarithm to the basee) if only one argument is provided. - If two arguments are provided, the
LOG()function computes the logarithm of the number to the specified base. - The first argument (
number) must be greater than0, and the second argument (base) must be greater than1for valid results.
Parameter Values
| Parameter | Description |
|---|---|
number |
Required. The number for which the logarithm is calculated (must be greater than 0). |
base |
Optional. The base of the logarithm (must be greater than 1). If omitted, the natural logarithm (base e) is used. |
Technical Details
- Works in: From MySQL 4.0
- Return Value: A floating-point value representing the logarithm of the number (based on the specified base).
Examples
Example 1: Return the natural logarithm of 2
SELECT LOG(2);
Result:
LOG(2): 0.693147
Explanation: The natural logarithm of 2 is approximately 0.693147.
Example 2: Return the logarithm of 4 with base 2
SELECT LOG(2, 4);
Result:
LOG(2, 4): 2
Explanation: The logarithm of 4 to the base 2 is 2, as 2^2 = 4.
Example 3: Return the natural logarithm of 10
SELECT LOG(10);
Result:
LOG(10): 2.302585
Explanation: The natural logarithm of 10 is approximately 2.302585.
Example 4: Return the logarithm of 1000 to base 10
SELECT LOG(10, 1000);
Result:
LOG(10, 1000): 3
Explanation: The logarithm of 1000 to the base 10 is 3, as 10^3 = 1000.
Related Functions
LN(): Returns the natural logarithm of a number (same asLOG()with basee).EXP(): Returnseraised to the power of the specified number.
Usage Notes
- The
LOG()function is widely used in scientific, financial, and statistical calculations that involve logarithmic growth, compounding, and other processes. - The second argument (
base) allows you to calculate logarithms to any base (e.g., base10for common logarithms). If omitted, the function defaults to the natural logarithm.