2013-05-02 75 views
6

我試圖從MySQL(total_points)中選擇一個值,然後添加到本地變量(new_points)並更新SAME查詢中的total_points,是否有人知道這是否可能...MySQL在一個查詢中選擇並更新

我現在有,

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

關於'UPDATE total_points =(total_points +%s)'這是哪個表?或者如果'total_points'是表格,那麼該字段是什麼? – reikyoushin 2013-05-02 17:23:34

回答

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

請注意查詢參數的順序進行了修訂。

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

plz使用代碼格式 – jurgenreza 2013-05-02 17:55:12