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);
  4. mysql> select c1 DIV 3 from t2;
  5. +----------+
  6. | c1 div 3 |
  7. +----------+
  8. | -1 |
  9. | 0 |
  10. +----------+
  11. 2 rows in set (0.00 sec)
  12. mysql> select c1 DIV c2 from t2;
  13. +-----------+
  14. | c1 div c2 |
  15. +-----------+
  16. | -1 |
  17. | 0 |
  18. +-----------+
  19. 2 rows in set (0.00 sec)