Tutorial by Examples

MySQL provides the following arithmetic operators OperatorNameExample+AdditionSELECT 3+5; -> 8 SELECT 3.5+2.5; -> 6.0 SELECT 3.5+2; -> 5.5-SubtractionSELECT 3-5; -> -2*MultiplicationSELECT 3 * 5; -> 15/DivisionSELECT 20 / 4; -> 5 SELECT 355 / 113; -> 3.1416 SELECT 10.0 / 0; -...
Pi The following returns the value of PI formatted to 6 decimal places. The actual value is good to DOUBLE; SELECT PI(); -> 3.141593
Angles are in Radians, not Degrees. All computations are done in IEEE 754 64-bit floating point. All floating point computations are subject to small errors, known as machine ε (epsilon) errors, so avoid trying to compare them for equality. There is no way to avoid these errors when using floating p...
Round a decimal number to an integer value For exact numeric values (e.g. DECIMAL): If the first decimal place of a number is 5 or higher, this function will round a number to the next integer away from zero. If that decimal place is 4 or lower, this function will round to the next integer value cl...
To raise a number x to a power y, use either the POW() or POWER() functions SELECT POW(2,2); => 4 SELECT POW(4,2); => 16
Use the SQRT() function. If the number is negative, NULL will be returned SELECT SQRT(16); -> 4 SELECT SQRT(-3); -> NULL
Generate a random number To generate a pseudorandom floating point number between 0 and 1, use the RAND() function Suppose you have the following query SELECT i, RAND() FROM t; This will return something like this iRAND()10.619143887068220.9384516830914230.83482678498591 Random Number in a r...
Return the absolute value of a number SELECT ABS(2); -> 2 SELECT ABS(-46); -> 46 The sign of a number compares it to 0. SignResultExample-1n < 0SELECT SIGN(42); -> 10n = 0SELECT SIGN(0); -> 01n > 0SELECT SIGN(-3); -> -1 SELECT SIGN(-423421); -> -1

Page 1 of 1