How do I multiply an unsigned int by -1 on a MySQL SELECT?


Let us first create a table −

mysql> create table DemoTable
   -> (
   -> Value int
   -> );
Query OK, 0 rows affected (0.80 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values(10);
Query OK, 1 row affected (0.17 sec)

mysql> insert into DemoTable values(20);
Query OK, 1 row affected (0.12 sec)

mysql> insert into DemoTable values(100);
Query OK, 1 row affected (0.14 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

Output

This will produce the following output −

+-------+
| Value |
+-------+
|    10 |
|    20 |
|   100 |
+-------+
3 rows in set (0.00 sec)

Here is the query to multiply an unsigned int by -1 on a SELECT −

mysql> select cast(Value*-1 as signed) from DemoTable;

Output

This will produce the following output −

+--------------------------+
| cast(Value*-1 as signed) |
+--------------------------+
|                      -10 |
|                      -20 |
|                     -100 |
+--------------------------+
3 rows in set (0.00 sec)

Updated on: 30-Jun-2020

84 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements