在table1中找到数据时更新Table2

时间:2013-05-15 03:21:41

标签: mysql sum where

请原谅我,如果我自欺欺人,但对于超出“基本”类型的mysql查询仍然相当新。目前我有两个数据表。

表1使用LOAD INTO解析数据,表2包含手动输入的用户信息数据。这是从|自动拉出的每天两次分隔文件。

以下是Table1中的列,它们包含我需要验证的数据(列数多于这些列):

sendID |  recID | transAmt | transDate | transStatus

以下列来自表2:

userID | userName | currentCapacity | maxCapacity

我想要做的是在userID或{{1}中找到curentCapacity的情况下运行userID sendID增加1的更新但仅限于recID。我尝试了以下内容:

transStatus = Active

这有效,UPDATE table1,table2 SET table2.currentCapacity=table2.currentCapacity+1 WHERE ( table2.transStatus="Active" AND (table2.userID = table1.sendID OR table1.recID)) 增加1,但只增加1。我想要的效果是将currentCapacity设置为等于所有currentCapacity =“有效”的总和。

我尝试了以下操作并返回“#1111 - 无效使用组功能”错误:

transStatus

有任何建议或指导吗?

1 个答案:

答案 0 :(得分: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