Tutorialspoint

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


1 Answer
Rama Giri

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;

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;

This will produce the following output −

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

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.