How do I update a table with old value + the new value?

ii iml0sto1

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:

enter image description here

Gordon Linoff

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.

edited at
0

Comments

0 comments
Login to comment

Related

How to create trigger that will insert into table old and new value on update/insert

How to compare old value and new value in triggers and then mark as insert/update/delete in audit table

Getting the old value of a table element, how can I get the new value?

Update the Old and New Value in Redux

How do I change an attribute on a struct to a new value which contains the old value?

UPDATE ... OUTPUT (old value = new value)

Laravel, how to run update on table column using old value

how do I use mod_rewrite to re-use old url param value in new redirect

Update values of a row to the max of old and new value

Laravel : How can i get old and new value by updateOrCreate

How do I update a value in SQL by looking up another table using that value?

How do I update one column value to contain all of the value in two columns of a table in MySQL?

How to update Sphinx Realtime Index with field = old_value + new_value?

SQLite- Update old value based on the new value

How do I update a value in table A when creating a row in table B?

How do I reference an attribute inside a table to a value inside a new row within the same table

How do I update a value in a dataframe in a loop?

How do I update a variable value in python?

How do I track the old value in a datagridview cell?

How to save old value and new value edited in a table to JSON in angular 2

How do I update a MongoDB document with new value using reactors Mono? (Kotlin)

How do I change the format of a value in a table?

How to use update to insert a new value in another table MySQL

How to migrate data to new table with some default value from old table in SQL

How to keep old value in MySQL on update of column?

How to replace an old value in a string with a new one?

How do I need to modify this MERGE statement, for a mass update of one field based on the value of a field in another table?

How do I update a json object value by referencing itself in a sqlite table?

How do i update various roles in a table with different value using php pdo statement