MySQL 查询求和另一个表中的值
我有几个具有父子关系的表。例如,我想将求和函数应用于子表的一列,并将其与父表的所有数据一起返回。
Parent_table
ID, Date, Title
Child_table
ID, another_id, column_to_sum
//(ID is foreign key pointing to Parent_table)
Sample Data in Parent_table
1, 22-11-2010 00:00:00 , 'Some Title'
2, 13-11-2010 00:00:00 , 'Some Title 2'
Sample Data in Child_table
1, 1, 10
1, 2, 11
1, 8, 3
2, 5, 11
2, 8, 6
查询的输出应返回 parent_table
中的所有列以及一个附加列,即对 Child_table
中的每个项目对 column_to_sum
的值进行求和Parent_table 与 ID 匹配。
如何?
I have got a couple of tables with a parent child relationship. I want to apply a sum function to one column of child table and return it with all data of parent table for example.
Parent_table
ID, Date, Title
Child_table
ID, another_id, column_to_sum
//(ID is foreign key pointing to Parent_table)
Sample Data in Parent_table
1, 22-11-2010 00:00:00 , 'Some Title'
2, 13-11-2010 00:00:00 , 'Some Title 2'
Sample Data in Child_table
1, 1, 10
1, 2, 11
1, 8, 3
2, 5, 11
2, 8, 6
Output of the query should return all columns in parent_table
with an additional column, that is, summing the values of column_to_sum
in Child_table
for each item in parent_table matched by ID.
How?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这并不能完全回答问题,但可能会对某人有所帮助,因为这是我的解决方案。 (我知道它缺少 JOIN)并且不知道该解决方案在大型数据库上的效率。
This does not exactly answer the question, but might help someone since this was my solution. (I know it's missing the JOIN) And have no idea about the efficiency of this solution on a large DB.
这超出了我的想象,但是怎么样?
This is off the top of my head, but how about