2013-05-15 51 views
0

請原諒我,如果我愚弄我自己,但仍然是對「基本」類以外的MySQL查詢的新手。目前我有兩個數據表。更新表2當數據在表1中找到時

表1使用LOAD INTO將數據解析爲數據,表2爲用戶信息手動輸入數據。這是從|自動拖動的分隔文件每天兩次。

以下是列於表1有我需要驗證對數據(也有不只是這些更多列):

sendID | recID | transAmt | transDate | transStatus 

而下面列從表2:

userID | userName | currentCapacity | maxCapacity 

我試圖做的是運行在userIDcurentCapacity增加一個,當userIDsendIDrecID被發現,但只有當更新。我已經試過如下:

UPDATE table1,table2 
SET table2.currentCapacity=table2.currentCapacity+1 
WHERE (table2.transStatus="Active" AND (table2.userID = table1.sendID OR table1.recID)) 

這工作和currentCapacity增加1,但也僅僅是增加了一個。我要去的效果是將currentCapacity設置爲等於所有transStatus =「有效」的總和。

我嘗試以下,並返回「#1111 - 無效使用組功能的」錯誤:

UPDATE table1,table2 
SET table2.currentCapacity=SUM(table1.transStatus) 
WHERE (table2.transStatus="Active" AND (table2.userID = table1.sendID OR table1.recID)) 

任何建議或指導?

回答

2

試試這個,如果它給你想要的結果,(沒有手動測試

UPDATE table2 b 
     INNER JOIN 
     (
      SELECT ID, SUM(transStatus = 'Active') total 
      FROM 
        (
         SELECT sendID ID, transStatus FROM table1 
         UNION ALL 
         SELECT recID ID, transStatus FROM table1 
        ) x 
      GROUP BY ID 
     ) a ON b.userID = a.ID 
SET  b.currentCapacity = a.total 
+0

我的假設是,第二選擇包含在FROM本來是要表1以及並改變它,運行查詢,它的工作原理!非常感謝! –

+0

你是對的。對於錯字':)'''抱歉 –