返回相似数据的两个表

6yoyoihd  于 2021-07-27  发布在  Java
关注(0)|答案(2)|浏览(260)

我已经尝试了我能想到的每一个连接,但无法解决:(
我有两张table
表x和表y
两个表都包含物料代码和数量
我要找的是能帮我比较的东西
理想情况下,我想看看这样的东西,有可能吗?
示例数据表x

+-----------+-----+
| Item Code | QTY |
+-----------+-----+
| A         | 1   |
+-----------+-----+
| B         | 2   |
+-----------+-----+
| C         | 3   |
+-----------+-----+

表y

+-----------+-----+
| Item Code | QTY |
+-----------+-----+
| A         | 1   |
+-----------+-----+
| D         | 2   |
+-----------+-----+
| E         | 3   |
+-----------+-----+

最终结果我想看到这样的东西:

+-----------+-------------+-------------+
| Item Code | TABLE X QTY | Table Y QTY |
+-----------+-------------+-------------+
| A         | 1           | 1           |
+-----------+-------------+-------------+
| B         | 2           | 0           |
+-----------+-------------+-------------+
| C         | 3           | 0           |
+-----------+-------------+-------------+
| D         | 0           | 2           |
+-----------+-------------+-------------+
| E         | 0           | 3           |
+-----------+-------------+-------------+

这可能是plz的帮助吗

d8tt03nd

d8tt03nd1#

这看起来像一个 full join ```
select coalesce(x.item, y.item) as item,
coalesce(x.qty, 0) as x_qty, coalesce(y.qty, 0) as y_qty
from x full join
y
on x.item = y.item

7ajki6be

7ajki6be2#

尝试使用 full outer join ...

SELECT
    ISNULL(X.[Item Code],Y.[Item Code]) AS [Item Code],
    ISNULL(X.QTY,0) AS [TABLE X QTY],
    ISNULL(Y.QTY,0) AS [TABLE Y QTY]
FROM X
    FULL OUTER JOIN Y
        ON X.[Item Code] = Y.[Item Code]

相关问题