LEFT JOIN 3 tables with group by 并获得总和
LEFT JOIN 3 tables with group by and get sum
我有 3 个表 t_customer、t_order 和 t_payment。
t_customer:
customer_id
customer_name
1
May
2
Jerry
t_order:
order_id
customer_id
order_amount
1
1
12.00
2
1
20.00
3
2
15.00
t_payment:
payment_id
customer_id
pay_amount
1
1
15.00
2
1
12.00
3
2
12.00
4
2
3.00
如何写sql得到如下结果?
customer_id
customer_name
SUM(order_amount)
SUM(pay_amount)
1
May
32.00
27.00
2
Jerry
15.00
15.00
我尝试左连接这3张表,结果如下。
customer_id
customer_name
order_amount
pay_amount
1
May
12.00
15.00
1
May
12.00
12.00
1
May
20.00
15.00
1
May
20.00
12.00
如您所见,如果我按 custom_id 对结果进行分组并对 order_amount 和 pay_amount 求和,结果将为 64.00 和 54.00。
这是我的 sql:
select tc.customer_id, custom_name, SUM(order_amount), SUM(pay_amount)
from t_customer tc
left join t_order t on tc.customer_id = t.customer_id
left join t_payment tp on tp.customer_id = tc.customer_id
group by tc.customer_id
您可以使用临时 table :
进行双连接
INSERT INTO t_final(customer_id,customer_name,order_amount) SELECT
c.customer_id,
c.customer_name,
SUM(o.order_amount)
FROM t_customer c
INNER JOIN t_order o ON c.customer_id = o.customer_id GROUP BY c.customer_id, c.customer_name;
UPDATE t_final SET pay_amount = (SELECT
SUM(p.pay_amount)
FROM t_payment p WHERE t_final.customer_id = p.customer_id GROUP BY t_final.customer_id LIMIT 1);
SELECT * FROM t_final;
您的查询存在的问题是,当您将 t_customer
与 t_order
和 t_payment
结合使用时,您会得到双重组合。查看此查询的输出以了解我的意思:
SELECT
c.customer_id,
c.customer_name,
o.order_amount,
p.pay_amount
FROM t_customer c
INNER JOIN t_order o ON c.customer_id = o.customer_id
INNER JOIN t_payment p ON c.customer_id = p.customer_id
为了避免这个问题,您可以将聚合操作移到连接操作之前:
SELECT
c.customer_id,
c.customer_name,
COALESCE(o.total_order_amount, 0) AS order_amount,
COALESCE(p.total_pay_amount, 0) AS pay_amount
FROM
t_customer c
LEFT JOIN (
SELECT customer_id,
SUM(order_amount) AS total_order_amount
FROM t_order
GROUP BY customer_id
) o ON c.customer_id = o.customer_id
LEFT JOIN (
SELECT customer_id,
SUM(pay_amount) AS total_pay_amount
FROM t_payment
GROUP BY customer_id
) p ON c.customer_id = p.customer_id
我有 3 个表 t_customer、t_order 和 t_payment。
t_customer:
customer_id | customer_name |
---|---|
1 | May |
2 | Jerry |
t_order:
order_id | customer_id | order_amount |
---|---|---|
1 | 1 | 12.00 |
2 | 1 | 20.00 |
3 | 2 | 15.00 |
t_payment:
payment_id | customer_id | pay_amount |
---|---|---|
1 | 1 | 15.00 |
2 | 1 | 12.00 |
3 | 2 | 12.00 |
4 | 2 | 3.00 |
如何写sql得到如下结果?
customer_id | customer_name | SUM(order_amount) | SUM(pay_amount) |
---|---|---|---|
1 | May | 32.00 | 27.00 |
2 | Jerry | 15.00 | 15.00 |
我尝试左连接这3张表,结果如下。
customer_id | customer_name | order_amount | pay_amount |
---|---|---|---|
1 | May | 12.00 | 15.00 |
1 | May | 12.00 | 12.00 |
1 | May | 20.00 | 15.00 |
1 | May | 20.00 | 12.00 |
如您所见,如果我按 custom_id 对结果进行分组并对 order_amount 和 pay_amount 求和,结果将为 64.00 和 54.00。
这是我的 sql:
select tc.customer_id, custom_name, SUM(order_amount), SUM(pay_amount)
from t_customer tc
left join t_order t on tc.customer_id = t.customer_id
left join t_payment tp on tp.customer_id = tc.customer_id
group by tc.customer_id
您可以使用临时 table :
进行双连接INSERT INTO t_final(customer_id,customer_name,order_amount) SELECT
c.customer_id,
c.customer_name,
SUM(o.order_amount)
FROM t_customer c
INNER JOIN t_order o ON c.customer_id = o.customer_id GROUP BY c.customer_id, c.customer_name;
UPDATE t_final SET pay_amount = (SELECT
SUM(p.pay_amount)
FROM t_payment p WHERE t_final.customer_id = p.customer_id GROUP BY t_final.customer_id LIMIT 1);
SELECT * FROM t_final;
您的查询存在的问题是,当您将 t_customer
与 t_order
和 t_payment
结合使用时,您会得到双重组合。查看此查询的输出以了解我的意思:
SELECT
c.customer_id,
c.customer_name,
o.order_amount,
p.pay_amount
FROM t_customer c
INNER JOIN t_order o ON c.customer_id = o.customer_id
INNER JOIN t_payment p ON c.customer_id = p.customer_id
为了避免这个问题,您可以将聚合操作移到连接操作之前:
SELECT
c.customer_id,
c.customer_name,
COALESCE(o.total_order_amount, 0) AS order_amount,
COALESCE(p.total_pay_amount, 0) AS pay_amount
FROM
t_customer c
LEFT JOIN (
SELECT customer_id,
SUM(order_amount) AS total_order_amount
FROM t_order
GROUP BY customer_id
) o ON c.customer_id = o.customer_id
LEFT JOIN (
SELECT customer_id,
SUM(pay_amount) AS total_pay_amount
FROM t_payment
GROUP BY customer_id
) p ON c.customer_id = p.customer_id