I have this SQL string which updates a row if it exists or creates a row if it does not, how do I do if it updates ctc_portfolio_coins_amount
If I have a value of 100, or -100, how do I either substract it or add it to the current value when it updates?
INSERT INTO ctc_portfolio_coins
(ctc_portfolio_coins_portfolio_fk, ctc_portfolio_coins_coin_fk, ctc_portfolio_coins_amount)
VALUES (1, 1, 100)
ON DUPLICATE KEY UPDATE ctc_portfolio_coins_amount = VALUES(ctc_portfolio_coins_amount)
Table:
Arithmetic:
INSERT INTO ctc_portfolio_coins (ctc_portfolio_coins_portfolio_fk, ctc_portfolio_coins_coin_fk, ctc_portfolio_coins_amount)
VALUES (1, 1, 100)
ON DUPLICATE KEY UPDATE
ctc_portfolio_coins_amount = ctc_portfolio_coins_amount + VALUES(ctc_portfolio_coins_amount);
The reference to ctc_portfolio_coins_amount
is the value in the column before the update. The reference to VALUES(ctc_portfolio_coins_amount)
is the value passed into the INSERT
statement.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments