我有两个表=借方和履历表。我想像这样在1行中合并2个表
查询是什么?
SELECT lao, SUM(outstanding) as Outstanding, COUNT(lao) as jumlah
FROM debitur
GROUP BY lao
和
SELECT SUM(tgt_pergeseran) as Target
FROM resume
GROUP BY lao
答案 0 :(得分:1)
尝试一下-
SELECT
A.lao,
A.Outstanding,
A.jumla,
B.Target
FROM
(
SELECT lao,
SUM(outstanding) as Outstanding,
COUNT(lao) as jumlah
FROM debitur
GROUP BY lao
)A
INNER JOIN
(
SELECT
lao,
SUM(tgt_pergeseran) as Target
FROM resume
GROUP BY lao
) B
ON A.lao = B.lao
答案 1 :(得分:0)
您可以在基于老挝语的两个查询之间使用联接,但是为此,您也需要老挝子查询选择
select
t1.lao, t1.Outstanding, t1.jumlah, t2.Target
from
(select
lao, sum(outstanding) as Outstanding, count(lao) as jumlah
from
debitur
group by
lao)
inner join
(select
lao, sum(tgt_pergeseran) as Target
from
resume
group by
lao) t2 on t2.lao = t1.lao
答案 2 :(得分:0)
MySQL不支持full join
。因此,如果您想要两个表中的所有记录(即使某些记录缺失),那么我建议使用union all
/ group by
:
SELECT lao, SUM(outstanding) as Outstanding,
COUNT(outstanding) as jumlah,
SUM(target) as target
FROM ((SELECT d.loa, d.outstanding, null as target
FROM debitur d
) UNION ALL
(SELECT r.loa, NULL, r.tgt_pergeseran as target
FROM resume r
)
) rd
GROUP BY lao ;