MySQL :: MySQL 8.4 Reference Manual :: 14.6.1 Arithmetic Operators (original) (raw)

14.6.1 Arithmetic Operators

Table 14.9 Arithmetic Operators

Name Description
%, MOD Modulo operator
* Multiplication operator
+ Addition operator
- Minus operator
- Change the sign of the argument
/ Division operator
DIV Integer division

The usual arithmetic operators are available. The result is determined according to the following rules:

These rules are applied for each operation, such that nested calculations imply the precision of each component. Hence,(14620 / 9432456) / (24250 / 9432456), resolves first to (0.0014) / (0.0026), with the final result having 8 decimal places (0.60288653).

Because of these rules and the way they are applied, care should be taken to ensure that components and subcomponents of a calculation use the appropriate level of precision. SeeSection 14.10, “Cast Functions and Operators”.

For information about handling of overflow in numeric expression evaluation, see Section 13.1.7, “Out-of-Range and Overflow Handling”.

Arithmetic operators apply to numbers. For other types of values, alternative operations may be available. For example, to add date values, use DATE_ADD(); see Section 14.7, “Date and Time Functions”.

mysql> SELECT 3+5;  
        -> 8  
mysql> SELECT 3-5;  
        -> -2  
mysql> SELECT - 2;  
        -> -2  

Note
If this operator is used with aBIGINT, the return value is also a BIGINT. This means that you should avoid using - on integers that may have the value of −263.

mysql> SELECT 3*5;  
        -> 15  
mysql> SELECT 18014398509481984*18014398509481984.0;  
        -> 324518553658426726783156020576256.0  
mysql> SELECT 18014398509481984*18014398509481984;  
        -> out-of-range error  

The last expression produces an error because the result of the integer multiplication exceeds the 64-bit range ofBIGINT calculations. (SeeSection 13.1, “Numeric Data Types”.)

mysql> SELECT 3/5;  
        -> 0.60  

Division by zero produces a NULL result:

mysql> SELECT 102/(1-1);  
        -> NULL  

A division is calculated withBIGINT arithmetic only if performed in a context where its result is converted to an integer.

mysql> SELECT 5 DIV 2, -5 DIV 2, 5 DIV -2, -5 DIV -2;  
        -> 2, -2, -2, 2