连接具有相同键但不同字段的两个表
Joining two tables with same keys but different fields
我有两个 table 都具有相同的字段 除了一个。我想将这两个 table 与生成的 table 结合起来,其中包含来自两者的所有字段,包括每个 table 中不相同的两个字段。即:
假设我有 table order_debit 架构
order_id, date_of_order, debit_balance
5, 2019, 100
2, 2021, 30
和 table order_credit 架构
order_id, date_of_order, credit_balance
1,2020, 35
2,2021, 40
我想要的是最终的 table 架构
order_id, date_of_order, credit_balance, debit_balance
5, 2019, 100, 0
2, 2021, 30, 40
1, 2020, 0, 35
我不知道如何在不获取重复值的情况下进行连接,我认为 order_ids 不会有太多重叠,但肯定会有一些。
您需要将 full join
与 coalesce
一起使用
select
coalesce(t1.order_id, t2.order_id) as order_id,
coalesce(t1.date_of_order, t2.date_of_order) as date_of_order,
coalesce(t1.debit_balance, 0) as debit_balance,
coalesce(t2.credit_balance, 0) as credit_balance
from t1
full join t2
on t1.order_id=t2.order_id
基本上你想要一个 full join
,MySQL 不支持。一个方便的方法是:
select od.order_id, od.date_of_order, od.debit_balance,
coalesce(oc.credit_balance, 0) as credit_balance
from order_debit od left join
order_credit oc
using (order_id, date_of_order)
union all
select oc.order_id, oc.date_of_order, 0, oc.credit_balance
from order_credit oc
where not exists (select 1
from order_debit od
where od.order_id = oc.order_id and od.date_of_order = oc.date_of_order
);
对于MySQL,您可以使用UNION ALL
和GROUP BY
SELECT
order_id, date_of_order, SUM(credit_balance), SUM(debit_balance)
FROM
(
SELECT order_id, date_of_order, 0 AS credit_balance, debit_balance FROM tbl_debit
UNION ALL
SELECT order_id, date_of_order, credit_balance, 0 AS debit_balance FROM tbl_credit
) FROM t
GROUP BY order_id, date_of_order
我有两个 table 都具有相同的字段 除了一个。我想将这两个 table 与生成的 table 结合起来,其中包含来自两者的所有字段,包括每个 table 中不相同的两个字段。即:
假设我有 table order_debit 架构
order_id, date_of_order, debit_balance
5, 2019, 100
2, 2021, 30
和 table order_credit 架构
order_id, date_of_order, credit_balance
1,2020, 35
2,2021, 40
我想要的是最终的 table 架构
order_id, date_of_order, credit_balance, debit_balance
5, 2019, 100, 0
2, 2021, 30, 40
1, 2020, 0, 35
我不知道如何在不获取重复值的情况下进行连接,我认为 order_ids 不会有太多重叠,但肯定会有一些。
您需要将 full join
与 coalesce
select
coalesce(t1.order_id, t2.order_id) as order_id,
coalesce(t1.date_of_order, t2.date_of_order) as date_of_order,
coalesce(t1.debit_balance, 0) as debit_balance,
coalesce(t2.credit_balance, 0) as credit_balance
from t1
full join t2
on t1.order_id=t2.order_id
基本上你想要一个 full join
,MySQL 不支持。一个方便的方法是:
select od.order_id, od.date_of_order, od.debit_balance,
coalesce(oc.credit_balance, 0) as credit_balance
from order_debit od left join
order_credit oc
using (order_id, date_of_order)
union all
select oc.order_id, oc.date_of_order, 0, oc.credit_balance
from order_credit oc
where not exists (select 1
from order_debit od
where od.order_id = oc.order_id and od.date_of_order = oc.date_of_order
);
对于MySQL,您可以使用UNION ALL
和GROUP BY
SELECT
order_id, date_of_order, SUM(credit_balance), SUM(debit_balance)
FROM
(
SELECT order_id, date_of_order, 0 AS credit_balance, debit_balance FROM tbl_debit
UNION ALL
SELECT order_id, date_of_order, credit_balance, 0 AS debit_balance FROM tbl_credit
) FROM t
GROUP BY order_id, date_of_order