0

[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.

asked Mar 29, 2019 at 9:44
0

1 Answer 1

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

answered Mar 29, 2019 at 12:10
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.