Use the MOD() function to divide the Amount values by 10 and then return the remainder from that division. : MOD « Math « SQL / MySQL






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)

   
    
    
    
  








Related examples in the same category

1.MOD in MySQL
2.The MOD() function returns the remainder derived by dividing two numbers.
3.modulus operator
4.Treat Monday as the first day of the week and Sunday as the last, use a the MOD( ) function to map Monday to 0