MySQL selects and updates in a single query

I am trying to select a value from MySQL (total_points) and then add to the local variable (new_points) and update the total_points in the EXACT query if anyone knows if this is possible ...

I currently have.,

cursor = database.cursor() cursor.execute("""SELECT total_points FROM g_ent WHERE e_id =%s AND user =%s; UPDATE total_points = (total_points + %s)""" ,(e_id, user_name, new_points)) database.commit() 
+6
source share
2 answers

The problem is that the SQL syntax is incorrect. The request should be:

 UPDATE g_ent SET total_points = total_points + %s WHERE e_id = %s AND user = %s; 

So a complete example:

 cursor = database.cursor() cursor.execute("""UPDATE g_ent SET total_points = total_points + %s WHERE e_id = %s AND user = %s;""", (new_points, e_id, user_name)) # order of params revised database.commit() 

Please note that the order of the request parameters has been changed.

+1
source
 UPDATE g_ent SET total_points = total_points + %s Where e_id = %s AND user = %s 
+3
source

Source: https://habr.com/ru/post/944141/


All Articles