2015-09-11 54 views
0

我已經試過這段代碼,它的工作原理,但只對每個客戶的第一個實例。即列應貫穿並將所有訂單添加到以前的欠款中。SQLite。在第一個例子中停止滿足WHERE條件

UPDATE customers_tbl 
    SET customer_amountowing = customer_amountowing + 
     (SELECT sorder_amount FROM standingorder_tbl 
      WHERE standingorder_tbl.sorder_customer = customers_tbl.customer_address1) 
    WHERE EXISTS 
     (SELECT * FROM standingorder_tbl 
      WHERE standingorder_tbl.sorder_customer = customers_tbl.customer_address1); 

有誰能告訴我因爲我在這裏錯了。乾杯傢伙!

+1

,如果你運行一個選擇具有相同返回什麼數據'WHERE EXISTS'條款? – Dai

+0

另外,請解釋你的模式。爲什麼'customer_address1'看起來是主鍵而不是'customerId'列? – Dai

回答

1

如果您要添加的所有以前的訂單總和的產品,則應使用SUM函數在子查詢以下列方式:

UPDATE customers_tbl 
    SET customer_amountowing = customer_amountowing + 
     (SELECT SUM(sorder_amount) FROM standingorder_tbl 
      WHERE standingorder_tbl.sorder_customer = customers_tbl.customer_address1) 
    WHERE EXISTS 
     (SELECT * FROM standingorder_tbl 
      WHERE standingorder_tbl.sorder_customer = customers_tbl.customer_address1); 
+0

非常感謝。問題是它在SQLIte管理器工具中工作,但是當我嘗試在我的android項目中使用原始查詢來實現它時,它不起作用。沒有崩潰,但是當我刷新SQLIte管理器時數據沒有更新。有什麼建議麼? – bez84

+0

啊。不要擔心,忘記Cursor.moveToFirst()。全部排序。謝謝@ uri2x – bez84