Arithmetic Operators

suggest change

MySQL provides the following arithmetic operators.

Operator Name Example
+ Addition SELECT 3+5; -> 8; SELECT 3.5+2.5; -> 6.0; SELECT 3.5+2; -> 5.5
- Subtraction SELECT 3-5; -> -2
* Multiplication SELECT 3 * 5; -> 15
/ Division SELECT 20 / 4; -> 5; SELECT 355 / 113; -> 3.1416; SELECT 10.0 / 0; -> NULL (warning; an error if ERROR_FOR_DIVISION_BY_ZERO is in effect in a strict context)
DIV Integer division SELECT 5 DIV 2; -> 2
% or MOD Modulo SELECT 7 % 3; -> 1; SELECT 15 MOD 4; -> 3; SELECT 15 MOD -4; -> 3; SELECT -15 MOD 4; -> -3; SELECT -15 MOD -4; -> -3; SELECT 3 MOD 2.5; -> 0.5

BIGINT

If the numbers in your arithmetic are all integers, MySQL uses the BIGINT (signed 64-bit) integer data type to do its work. For example:

SELECT (1024 * 1024 * 1024 * 1024 * 1024 * 1024) + 1; -> 1152921504606846977

and

SELECT (1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024); -> BIGINT out-of-range error (since MySQL 5.5.5; earlier versions saturated the result)

DOUBLE

If any numbers in your arithmetic are fractional, MySQL uses 64-bit IEEE 754 floating point arithmetic. You must be careful when using floating point arithmetic, because many floating point numbers are, inherently, approximations rather than exact values.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents