The MySQL FLOOR function is a mathematical function used to round a numeric value down to the nearest integer that is less than or equal to the original value. It is particularly useful when you want to discard the decimal part of a number and obtain the largest integer that is less than or equal to the given value.
Syntax
The basic syntax of the FLOOR function is as follows:
FLOOR(x)
Here, “x” is the numeric expression or column that you want to round down to the nearest integer. The result of the FLOOR function is always of the same data type as the input value.
Example
Let’s look at a simple example to illustrate the usage of the FLOOR function:
Suppose you have a table named “prices” with a column called “amount” that stores various amounts of money, including decimal values. You want to retrieve the floor value for each amount. You can use the FLOOR function as follows:
SELECT amount, FLOOR(amount) AS floor_value FROM prices;
In this example, the FLOOR function will round down each “amount” to the nearest integer, and the result will be displayed alongside the original values in the “floor_value” column.
Keep in mind that the FLOOR function can be helpful in scenarios where you need to perform calculations with whole numbers or when dealing with data that should be represented in integer form. It is part of a broader set of mathematical functions available in MySQL, providing developers with the tools to manipulate numeric data effectively.