比较 SQL Server 中 2 个不同表的父子组合 [英] Comparing Parent-child combination from 2 different tables in SQL Server
问题描述
我有表 MAT_ITEM
和数据如下
+--------+-------+
| MAT_NO | CHILD |
+--------+-------+
| 9856 | 874 |
| 9856 | 856 |
| 9856 | 548 |
| 9883 | 596 |
| 9883 | 356 |
| 7845 | 101 |
| 7845 | 908 |
| 7845 | 206 |
+--------+-------+
另一个表MAT_REL
和数据如下:
+--------+----------+----------+
| MAT_NO | PARENT | CHILD |
+--------+----------+----------+
| 9856 | | STEEL |
| 9856 | STEEL | 874 |
| 9856 | STEEL | 856 |
| 9856 | STEEL | 548 |
| 9856 | A-STEEL | 874 |
| 9856 | B_STEEL | 856 |
| 7845 | | METAL |
| 7845 | O_METAL | 102 |
| 7845 | I_METAL | 908 |
| 7845 | METAL | 102 |
| 7845 | METAL | 908 |
| 7845 | METAL | 206 |
| 7845 | METAL | 769 |
| 9883 | | CARBON |
| 9883 | B_CARBON | 596 |
| 9883 | C_CARBON | 356 |
| 9883 | CARBON | 596 |
| 9883 | CARBON | 147 |
+--------+----------+----------+
基本上 MAT_REL
中带有空 PARENT
的行被认为是 TOP Parent,它的孩子将被视为 PARENT
用于我在MAT_REL
.MAT_REL
也可能包含 Parent 作为其他值(例如,A_STEEL、B_CARBON 等,),我并不担心.MAT_REL
中的类似内容是我正在考虑进行比较的内容.
Basically the row with empty PARENT
in MAT_REL
is considered as the TOP Parent and it's child will be considered as PARENT
for my comparison in MAT_REL
. MAT_REL
may contain Parent as other values as well (eg., A_STEEL,B_CARBON etc.,) which I'm not worried about.
Something like this in MAT_REL
is what I'm considering for comparison.
+--------+--------+-------+
| MAT_NO | PARENT | CHILD |
+--------+--------+-------+
| 9856 | STEEL | 874 |
| 9856 | STEEL | 856 |
| 9856 | STEEL | 548 |
| 9883 | CARBON | 596 |
| 9883 | CARBON | 147 |
| 7845 | METAL | 102 |
| 7845 | METAL | 908 |
| 7845 | METAL | 206 |
| 7845 | METAL | 769 |
+--------+--------+-------+
现在我想比较 MAT_ITEM
和 MAT_REL
是否 MAT_NO &
与 MAT_ITEM
组合中的 CHILDMAT_NO
、PARENT
和 PARENT
相同MAT_REL
中的 CHILD
.我正在尝试获取不匹配的行.我们不能直接比较 MAT_ITEM
和 MAT_REL
.直接比较是行不通的,因为如果你看到 MAT_NO 9883 ,直接比较可能会给出结果,因为行是相同的,但我们必须检查对于 CARBON 而不是其他(B_CARBON)作为整体
Now I want to compare MAT_ITEM
and MAT_REL
whether the MAT_NO & CHILD
in MAT_ITEM
combo is same as MAT_NO
,PARENT
& CHILD
in MAT_REL
.I'm trying to get the non-matching rows. We cannot directly compare MAT_ITEM
and MAT_REL
.Direct comparison will not work because if you see for MAT_NO 9883 , direct comparison might give the result as rows are same but we have to check for CARBON and not others(B_CARBON) as overall
预期输出:(有或没有孩子)
Expected output : (with or without child)
9883
7845
我可以获得单个 MAT_NO 的详细信息.
I am able to get the detail for single MAT_NO.
SELECT * FROM MAT_ITEM WHERE MAT_NO='7845'
SELECT * FROM MAT_REL
WHERE MAT_NO = '7845' AND PARENT IS NULL -- METAL (using this below)
SELECT * FROM MAT_REL
WHERE MAT_NO = '7845' AND PARENT = 'METAL'
SELECT DISTINCT CHILD FROM MAT_ITEM WHERE MAT_NO = '7845'
EXCEPT
SELECT DISTINCT CHILD FROM MAT_REL
WHERE MAT_NO = '7845' AND PARENT = 'METAL' -- will return some rows --101
但不确定如何处理整套组合.
but not sure how to do for whole set of combinations.
推荐答案
根据对问题的评论回答.我在 SQLite 数据库上进行了尝试,由于与 SQLServer 相比语法可能有所不同,因此我只能给您指示.我没有数据库链接.
Answer as per comments on the question. I tried it out on a SQLite database, and since the syntax may be off compared to SQLServer, I can only give you directions. I do not have a link to a database.
寻找基础:
- 在 mat_rel 中找到 mat_no, child 对,其中 parent 为 NULL
- 在 mat_rel 中查找与 mat_no 匹配且父级与 1 中的子级匹配的所有行.在类似 (SELECT ..) 的 JOIN 中添加来自 1. 的 SQL.
查找不匹配(比较 child=child 和 mat_no=mat_no):
Finding mismatch (comparing child=child and mat_no=mat_no):
- 查找 2 中的所有行,其中 mat_item 中没有匹配的行.使用 LEFT JOIN 或 NOT EXISTS
- 在 mat_item 中查找 2 中 mat_rel 中没有匹配行的所有行.使用 RIGHT JOIN 或 NOT EXISTS.
同时找到 3 和 4:
Finding both 3 and 4:
- 将两个 SQL 与 UNION ALL 一起使用
这篇关于比较 SQL Server 中 2 个不同表的父子组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!