Mysql: how to convert a positive value to negative when pasting

I have one table transaction with columns (id, amount) of all positive integers, and I have another table with the same columns, but here the sum will be negative. Now I want to use the Paste into command command to select from a transaction and insert into a return. How can I make a negative value when pasting. The next query I'm trying to do.

INSERT INTO refund (id,amount) SELECT id, amount FROM `transaction` 

Is there a way refund.amount always accepts -ve defaults.

+5
source share
4 answers
 INSERT INTO refund (id,amount) SELECT id, (0 - amount) as amount FROM `transaction` 

since all data in transaction positive.

 INSERT INTO refund (id,amount) SELECT id, (-1 * amount) as amount FROM `transaction` 
+8
source

If for some reason you are not guaranteed, you get a positive amount every time you insert a new row, take an absolute value and multiply it by -1

 INSERT INTO refund (id,amount) SELECT id, -1 * ABS(amount) FROM `transaction` 

Otherwise, the ABS() part ABS() not needed.

 INSERT INTO refund (id,amount) SELECT id, -1 * amount FROM `transaction` 
+7
source

Just multiply it by -1, do it as part of the insert statement.

 INSERT INTO refund (id,amount) SELECT id, amount * -1 FROM `transaction` 
+3
source
 INSERT INTO refund (id,amount) SELECT id, (amount * -1) AS amount FROM `transaction` 
+3
source

All Articles