Mega Code Archive

 
Categories / MySQL / Math
 

Use the MOD() function to divide the Amount values by 10 and then return the remainder from that division

mysql> CREATE TABLE Test     -> (     ->     TestID SMALLINT NOT NULL PRIMARY KEY,     ->     Amount SMALLINT NOT NULL     -> ); Query OK, 0 rows affected (0.01 sec) mysql> INSERT INTO Test VALUES     -> (101, 12),     -> (102, 1),     -> (103, 139),     -> (104, -37),     -> (105, 0),     -> (106, -16); Query OK, 6 rows affected (0.00 sec) Records: 6  Duplicates: 0  Warnings: 0 mysql> mysql> mysql> SELECT TestID, Amount, MOD(Amount, 10) AS Modulo     -> FROM Test     -> ORDER BY TestID; +--------+--------+--------+ | TestID | Amount | Modulo | +--------+--------+--------+ |    101 |     12 |      2 | |    102 |      1 |      1 | |    103 |    139 |      9 | |    104 |    -37 |     -7 | |    105 |      0 |      0 | |    106 |    -16 |     -6 | +--------+--------+--------+ 6 rows in set (0.00 sec) mysql> mysql> mysql> drop table test; Query OK, 0 rows affected (0.00 sec)