mysql从另一个表更新表
我正在尝试根据另一个表中另一个字段的总和来更新一个表中的字段。
company_tbl(主要、公司规模、公司名称) location_tbl (PRIMARY、companyID、locationSize、locationName)
两个表通过 company_tbl.PRIMARY = location_tbl.companyID 链接
update company_tbl comp, location_tbl loc
set companySize = sum(locationSize)
where comp.PRIMARY = loc.companyID
我收到“无效使用组函数”的错误
公司可以有多个位置
我想要做的可能吗?我想获取属于特定公司的地点的总和,并用总和更新 companySize。
谢谢!
I'm trying to update a field in one table, from the sum of another field, in another table.
company_tbl (PRIMARY, companySize, companyName)
location_tbl (PRIMARY, companyID, locationSize, locationName)
The two tables link by company_tbl.PRIMARY = location_tbl.companyID
update company_tbl comp, location_tbl loc
set companySize = sum(locationSize)
where comp.PRIMARY = loc.companyID
I'm getting an error of 'invalid use of group function'
A company can have multiple locations
Is what I want to do possible? I want to take the sum of locations, that belong to a specific company, and update the companySize with the sum.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用:
...或者你可以使用视图 ,包含:
Use:
...or you could use a view, containing:
首先将 companySize 初始化为零:
然后对于每个匹配地点行,添加 locationSize:
在处理完每个公司的所有匹配地点时,您将获得所需的总和。
First initialize the companySize to zero:
Then for each matching location row, add the locationSize:
You get the desired sum by the time it has processed all the matching locations for each company.