I'll explain some of the mathematical concept of the ABS () , because I believe it's what you want to understand, and it's what's part of the site's scope.
ABS () - returns the absolute value of a given number
What is an absolute value this small explanation already gives an introduction:
The absolute value of any number refers to its magnitude and not to the signal it can have, whether positive or negative. In mathematical terms, the absolute value is an operation that allows any number to become positive. / p>
That's why when you use this ABS function, as you mentioned:
mysql> SELECT ABS(2);
-> 2
mysql> SELECT ABS(-32);
-> 32
It will return, respectively, the values 2
and 32
. Basically always returns the positive value, regardless of which number is X.
This function ABS()
, is closely related to the module concept ( 1 , 2 ). Actually, because it's the same function but with another name.
Remember, a lot of time when you studied at school, and your teacher placed between "bars" the number to find the module " |x|
:
|4|=4
or |-3|=3
Basically, it is this concept of having the nonnegative value of X, regardless of its signal, only applied to the database.
Therefore, | x | = x, if x is a positive number and | x | = -x, if x is a negative number ....
Some other examples of outputs using ABS()
:
SELECT ABS(3);
-> 3
SELECT ABS(-7);
-> 7
SELECT ABS(0);
-> 0
SELECT ABS(-1);
-> 1