[Migrated from Stackoverflow]
I have the following statement in Python for MySQL Driver, that increments counter by one, on update with the same language.
add_lang = ("INSERT INTO Languages (lang, length, counter)\
VALUES (%s, %s, 1)\
ON DUPLICATE KEY UPDATE\
counter = counter + 1;")
Now, what I want to achieve is a cumulative sum. My analogy suggests:
add_lang = ("INSERT INTO Languages (lang, length, counter)\
VALUES (%s, 0, 1)\
ON DUPLICATE KEY UPDATE\
counter = counter + 1;\
length = length + new_length")
How to differentiate between inserted new values, and current value before update ? Notice new_length and length. this is just to show my aim.
1 Answer 1
Try this:
add_lang = ("INSERT INTO Languages (lang, length, counter)\
VALUES (%s, %s, 1)\
ON DUPLICATE KEY UPDATE\
counter = counter + 1,\
length = length + VALUES(length);")
NB: edited to apply parameter replacement to the VALUES
lang-sql