MySQL在一个查询中选择并更新

时间:2013-05-02 17:19:03

标签: mysql mysql-python

我正在尝试从MySQL中选择一个值(total_points)然后添加到局部变量(new_points)并更新SAME查询中的total_points,是否有人知道这是否可行......

我目前有。,

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()    

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:1)

问题是您的SQL语法不正确。查询应为:

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

完整的例子是:

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()

请注意,查询参数的顺序已修订。