如果我自欺欺人,但是对MySQL查询而言,除“基本”种类之外还相当陌生,请原谅。目前,我有两个数据表。
表1具有使用LOAD INTO解析的数据,表2具有为用户信息手动输入的数据。这是从|中自动拉出的。分隔文件,每天两次。
以下是Table1中的列,这些列具有我需要验证的数据(除了这些列之外,还有更多列):
sendID | recID | transAmt | transDate | transStatus
以下几列来自表2:
userID | userName | currentCapacity | maxCapacity
我要执行的操作是运行更新,当在
userID
或curentCapacity
中找到userID
时,将sendID
recID
增加一个,但仅当transStatus = Active
时。我尝试了以下方法:UPDATE table1,table2
SET table2.currentCapacity=table2.currentCapacity+1
WHERE ( table2.transStatus="Active" AND (table2.userID = table1.sendID OR table1.recID))
此方法有效,并且
currentCapacity
增加1,但仅增加1。我要实现的效果是将currentCapacity
设置为等于所有transStatus
=“ Active”的总和。我尝试了以下操作,并返回“#1111-组功能的无效使用”错误:
UPDATE table1,table2
SET table2.currentCapacity=SUM(table1.transStatus)
WHERE ( table2.transStatus="Active" AND (table2.userID = table1.sendID OR table1.recID))
有什么建议或指导吗?
最佳答案
如果可以提供您想要的结果,请尝试一下(未经手动测试)
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