6

我正在尝试从 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()    
4

2 回答 2

3
UPDATE g_ent
SET total_points = total_points + %s
Where e_id = %s 
  AND user = %s
于 2013-05-02T17:53:59.510 回答
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()

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

于 2013-05-02T17:27:54.803 回答