DIV

Description

The DIV operator is used for integer division. Discards from the division result any fractional part to the right of the decimal point.

If either operand has a non-integer type, the operands are converted to DECIMAL and divided using DECIMAL arithmetic before converting the result to BIGINT. If the result exceeds BIGINT range, an error occurs.

Syntax

1. > SELECT value1 DIV value2;
1. > SELECT column1 DIV column2... FROM table_name;

Examples

1. mysql> SELECT 5 DIV 2, -5 DIV 2, 5 DIV -2, -5 DIV -2;
2. +---------+----------+----------+-----------+
3. | 5 div 2 | -5 div 2 | 5 div -2 | -5 div -2 |
4. +---------+----------+----------+-----------+
5. |       2 |       -2 |       -2 |         2 |
6. +---------+----------+----------+-----------+
7. 1 row in set (0.00 sec)
1. create table t2(c1 int, c2 int);
2. insert into t2 values (-3, 2);
3. insert into t2 values (1, 2);

5. mysql> select c1 DIV 3 from t2;
6. +----------+
7. | c1 div 3 |
8. +----------+
9. |       -1 |
10. |        0 |
11. +----------+
12. 2 rows in set (0.00 sec)

14. mysql> select c1 DIV c2 from t2;
15. +-----------+
16. | c1 div c2 |
17. +-----------+
18. |        -1 |
19. |         0 |
20. +-----------+
21. 2 rows in set (0.00 sec)