
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to convert positive value to negative while inserting in MySQL?
Let us first create a table
mysql> create table recordsDemo -> ( -> UserId int, -> Value int -> ); Query OK, 0 rows affected (0.52 sec)
Now insert some records in the table using insert command.
The query is as follows
mysql> insert into recordsDemo values(1,10); Query OK, 1 row affected (0.17 sec) mysql> insert into recordsDemo values(3,598); Query OK, 1 row affected (0.18 sec) mysql> insert into recordsDemo values(5,786); Query OK, 1 row affected (0.25 sec) mysql> insert into recordsDemo values(7,189); Query OK, 1 row affected (0.16 sec) mysql> insert into recordsDemo values(9,345); Query OK, 1 row affected (0.14 sec)
Display all records from the table using select statement.
The query is as follows
mysql> select *from recordsDemo;
The following is the output
+--------+-------+ | UserId | Value | +--------+-------+ | 1 | 10 | | 3 | 598 | | 5 | 786 | | 7 | 189 | | 9 | 345 | +--------+-------+ 5 rows in set (0.00 sec)
Create a second table. The query to create a second table is as follows
mysql> create table PositiveToNegativeValueDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Money int -> ); Query OK, 0 rows affected (0.83 sec)
Here is the query to convert positive value to negative while inserting
mysql> insert into PositiveToNegativeValueDemo(Id,Money) -> select UserId,(-1*Value) from recordsDemo; Query OK, 5 rows affected (0.15 sec) Records: 5 Duplicates: 0 Warnings: 0
Now check table records from the table using select statement.
The query is as follows
mysql> select *from PositiveToNegativeValueDemo;
The following is the output
+------+-------+ | Id | Money | +------+-------+ | 1 | -10 | | 3 | -598 | | 5 | -786 | | 7 | -189 | | 9 | -345 | +------+-------+ 5 rows in set (0.00 sec)
- Related Articles
- Java Program to convert positive int to negative and negative to positive
- How to fix the incorrect datetime value while inserting in a MySQL table?
- How to convert a negative number to a positive one in JavaScript?
- How to convert a positive image to Negative to using OpenCV library?
- How to convert negative values in an R data frame to positive values?
- MySQL query to first set negative value in descending order and then positive value in ascending order
- How to convert a negative image to positive image using Java OpenCV library?
- Format date while inserting records in MySQL
- Converting boolean values to positive or negative sign in MySQL?
- How to change negative numbers to positive in Excel?
- Set particular value of column without using update and while inserting values in MySQL
- Convert negative denominator into positive:$\frac{5}{-3}$
- How to avoid inserting duplicate rows in MySQL?
- How to print positive and negative infinity values in JavaScript?
- Counting number of positive and negative votes in MySQL?

Advertisements