基于mysql中的子查询更新几行
这是我想要做的事情的简化表结构:
T1:
id,uid,组,值1
1,1,A,3
2,1,B,4
3,2,A,6
4,2,B,7
现在我希望任何同时具有 A 组和 B 组行的 uid 与 B 组关联的值将被添加到与 B 组关联的行中的值。
所以上面的结果将是
1,1,A,7 (3+4)
2,1,B,4
3,2,A,13 (6+7)
4,2,B,7
也可能存在仅存在组 A 或仅存在组 B 的 uid,在这种情况下,不应对这些行进行任何更改。
到目前为止,我所做的是创建一个查询,其中一行对应需要更新的任何行:
id、uid、newval
1,1,7
3,2,13
子查询有点复杂,它连接了需要更新的表。
现在我该如何使用它来更新原始表? (或者我该如何以其他方式做到这一点)?
谢谢。
Here is a simplified table structre for what I am trying to do:
T1:
id,uid,group,value1
1,1,A,3
2,1,B,4
3,2,A,6
4,2,B,7
now I want that any uid that has both a row for group A and group B the value assosicated with group B will be added to the value in the row associated with Group B.
so the result for the above would be
1,1,A,7 (3+4)
2,1,B,4
3,2,A,13 (6+7)
4,2,B,7
there might also be uids for which only group A exists or only groups B exists in which case no changes t those lines should take place.
What I did so far is that I created a query that has one row for any row that needs to be updated:
id,uid,newval
1,1,7
3,2,13
The subquery is somewhat complex and joins the table that needs to be updated with it self.
now how do I use this to update the original table? (or how do I do it some other way)?
thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)