How to update field to add value to existing value?

前端 未结 7 1956
迷失自我
迷失自我 2021-01-03 18:11

How to update field to add value to existing value?
For example I have

Table name: table

id   credit
1      4
2      5
3      3
         


        
7条回答
  •  北荒
    北荒 (楼主)
    2021-01-03 18:59

    I wanted to add to this with an 'ON DUPLICATE KEY UPDATE' example(based on the answer by @hims056). I found this answer but needed 'ON DUP...' so I figured may as well post it here.

    INSERT INTO table1 
    (`id`, `credit`)
    VALUES (1, 4)
    ON DUPLICATE KEY UPDATE
    `credit` = `credit` + 7;
    

    See the SQL Fiddle here

提交回复
热议问题