I asked this question yesterday but It didn’t really help me. I am asking it again hoping my question is more clear this time. I am trying to update multiple rows in MySQL by using WHEN, THEN, ELSE case. I am trying to achieve this using python and Django in the backend. Here is my code:
UPDATE priority
SET priority_order =
CASE priority_order
WHEN 4 THEN 8
WHEN 1 THEN 4
WHEN 3 THEN 2
## More X amount of WHEN/THEN statements
ELSE priority_order
END
The problem is that I do not know how many WHEN and THEN lines I have.. How can I construct such a statement so that I can execute this using cursor.execute( )?
Yesterday, someone advised to build a list of expression and pass as an argument like:
from django.db.models import Case, When
# Make a list of When expressions
when_list = [When(priority_number=2, then=3),
When(priority_number=3, then=4)]
# Use in a query of your choice, as an example:
Model.objects.update(
priority_number=Case(
*when_list))
But seems like it does not work on Mysql. I think this only works on SQLLite. The *when_list is not being decoded as my query execute. Please help!