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.

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

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.