In MySQL, the LOG10()
function returns the base-10 logarithm of a specified value.
You provide the specified value as an argument when calling the function.
Syntax
This syntax goes like this:
LOG10(X)
Where X
is the value for which you want the base-10 logarithm returned.
If X
is less than or equal to 0.0E0, the function returns NULL
and a warning is generated.
Example 1 – Basic Usage
Here’s a basic example to demonstrate how this function works.
SELECT LOG10(10);
Result:
+-----------+ | LOG10(10) | +-----------+ | 1 | +-----------+
And here’s another example using various values.
SELECT LOG10(100), LOG10(1000), LOG10(3000), LOG10(10000);
Result:
+------------+-------------+--------------------+--------------+ | LOG10(100) | LOG10(1000) | LOG10(3000) | LOG10(10000) | +------------+-------------+--------------------+--------------+ | 2 | 3 | 3.4771212547196626 | 4 | +------------+-------------+--------------------+--------------+
Example 2 – Negative Value
Here’s an example of passing in a negative value.
SELECT LOG10(-4);
Result:
+-----------+ | LOG10(-4) | +-----------+ | NULL | +-----------+ 1 row in set, 1 warning (0.00 sec)
This example returns a NULL value (and a warning) because the argument provided is less than 0.0E0.
Example 3 – Zero
Here’s an example of passing in zero as the argument (we get the same result as the previous example).
SELECT LOG10(0);
Result:
+----------+ | LOG10(0) | +----------+ | NULL | +----------+ 1 row in set, 1 warning (0.00 sec)
Example 4 – Expressions
You can also pass in expressions like this:
SELECT LOG10(5*2);
Result:
+------------+ | LOG10(5*2) | +------------+ | 1 | +------------+
LOG()
There’s also the LOG()
function (and its synonym LN()
), which returns the natural logarithm of a number.
In fact, you can use LOG()
instead of LOG10()
if you prefer. To do this, use 10
as the first argument to the function. Example below.
SELECT LOG(10, 100), LOG10(100);
Result:
+--------------+------------+ | LOG(10, 100) | LOG10(100) | +--------------+------------+ | 2 | 2 | +--------------+------------+
LOG2()
MySQL also has the LOG2()
function that returns the base-2 logarithm of a value.