Upsert in SQLite with running total if record is found

后端 未结 1 684
小蘑菇
小蘑菇 2021-01-28 13:33

I would like to upsert data to a table in SQLite, but the column being updated (if the record is found) must be a running total. I am using a parameterized query, and am having

相关标签:
1条回答
  • 2021-01-28 13:44

    OK so this is working for me:

    INSERT OR REPLACE INTO TABLE (ID, Quantity) 
    VALUES (:ID, 
            COALESCE(
              (SELECT Quantity FROM TABLE WHERE ID = :ID) + :Quantity_change, :Quantity_change));
    

    Thanks to this SO link for the help.

    0 讨论(0)
提交回复
热议问题