gpt4 book ai didi

mysql - 当表1中找到数据时更新表2

转载 作者:行者123 更新时间:2023-11-29 08:31:12 24 4
gpt4 key购买 nike

如果我出丑了,但对“基本”类型之外的 mysql 查询仍然相当陌生,请原谅我。目前我有两个数据表。

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

表 1 中的以下列包含我需要验证的数据(还有更多列):

sendID |  recID | transAmt | transDate | transStatus

以下列来自表2:

userID | userName | currentCapacity | maxCapacity

我想要做的是运行更新,当在以下位置找到 userID 时,userID currentCapacity 增加 1: sendIDrecID 但仅限于 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

关于mysql - 当表1中找到数据时更新表2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16556253/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com