[英]MySQL: How do I select two fields from table, and total of amount columns in two related tables?
I have three tables: 我有三个表:
Project
PurchaseA
PurchaseB
Project
PurchaseA
PurchaseB
Schema of Project: 项目架构:
id | user_id | title | description | create_date
Schema of PurchaseA: 购买方案A:
id | project_id | amount | create_date
Schema of PurchaseB: PurchaseB模式:
id | project_id | amount | create_date
Project
has a bunch of unique rows detailing different projects. Project
有一堆独特的行,详细说明了不同的项目。 Each of these projects has many rows in both PurchaseA
and PurchaseB
. 这些项目中的每个项目在
PurchaseA
和PurchaseB
都有很多行。 What I want to do is select all of the rows from Project
with the fields id
and title
, but also the SUM of both the total of the related records in PurchaseA
and the total of the related records in PurchaseB
. 我想要做的是选择所有从行的
Project
与领域的id
和title
,也都总在相关记录的总和PurchaseA
和总的相关记录PurchaseB
。
I can get it working sort of independently of each other, but when I have two joins, the numbers end up being massively inflated. 我可以使它彼此独立地工作,但是当我有两个联接时,数字最终会大大膨胀。
Desired Result 所需结果
21732 Test Project A 84274.48
21747 Test Project B 18237.47
34714 Test Project C 183747.18
Where the column with the FLOATS is the sum of the related rows from PurchaseA
and PurchaseB
. 其中带有FLOATS的列是
PurchaseA
和PurchaseB
相关行的总和。
Edit 编辑
Here is my current SQL: 这是我当前的SQL:
select
projects.id,
projects.title,
COALESCE(SUM(PurchaseA.amount), 0) as pledgetotal,
COALESCE(SUM(PurchaseB.amount), 0) as purchasetotal
from
projects
left join
PurchaseA ON (PurchaseA.project_id = projects.id)
left join
PurchaseB ON (PurchaseB.project_id = projects.id)
left join
WHERE
projects.title IS NOT NULL
projects.end_date < NOW()
group by projects.id
Test Data 测试数据
Projects:
id title description create_date
623 Test Project A This is a test A 2013-01-01
624 Test Project B This is a test B 2013-01-02
PurchaseA
id project_id amount create_date
1 623 100 2013-01-02
2 623 125 2013-01-02
3 624 200 2013-01-03
4 623 150 2013-01-03
5 624 50 2013-01-04
PurchaseB
id project_id amount create_date
1 623 110 2013-01-02
2 624 105 2013-01-02
3 623 215 2013-01-03
4 623 100 2013-01-03
5 624 150 2013-01-04
So as you can see, each project has multiple rows in each related table. 如您所见,每个项目在每个相关表中都有多个行。 I want to get the sums from each table, then add those together.
我想从每个表中获取总和,然后将它们加在一起。
I believe part of your problem is from the one-to-many relationship that exists between the projects
and the purchase.
我相信您的问题的部分原因在于
projects
与purchase.
之间存在一对多的关系purchase.
tables. 表。 My suggestion would be to move the
sum()
to subqueries: 我的建议是将
sum()
移到子查询:
select
p.id,
p.title,
a.pledgetotal,
b.purchasetotal
from projects p
left join
(
select project_id, sum(amount) pledgetotal
from PurchaseA
group by project_id
) a
ON (a.project_id = p.id)
left join
(
select project_id, sum(amount) purchasetotal
from PurchaseB
group by project_id
) b
ON (B.project_id = p.id)
WHERE p.title IS NOT NULL;
You can try this (not tested): 您可以尝试以下操作(未经测试):
SELECT a.id, a.title, (pa.amount+pb.amount) AS TotalPurchase FROM Project a
LEFT JOIN ( SELECT project_id, SUM(amount) AS amount FROM PurchaseA GROUP BY project_id ) pa
ON pa.project_id = a.id
LEFT JOIN ( SELECT project_id, SUM(amount) AS amount FROM PurchaseB GROUP BY project_id ) pb
ON pb.project_id = a.id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.