我有一个表格,我用代码选择:代码A
Select TDS, TL, IK
From (Select Sheet1.TOOLING_DATA_SHEET As TDS, Sheet1.CUTTING_TOOL As TL, ENT_ITEM_MASTER.ITEM_KEY As IK
From Sheet1
Inner Join ENT_ITEM_MASTER
On ENT_ITEM_MASTER.ITEM_CODE=Sheet1.CUTTING_TOOL And ENT_ITEM_MASTER.USER_LAST_MODIFIED Is Not Null) As A
输出:
TDS TL IK
TDS-1980D-10+OP10+S7 TL-000032 1
TDS-1980D-10+OP10+S7 TL-000019 34
TDS-2258-01+OP10+S4 TL-000016 53
TDS-2325PU+OP10+S1 TL-000036 7
TDS-1234-56-78 TL-000123 45
和我用代码选择的另一个表:代码B
Select ENT_LINK_OBJECTS.OBJ_NAME, ENT_ITEM_MASTER.ITEM_CODE, ENT_ITEM_MASTER.ITEM_KEY
From ENT_LINK_OBJECTS
Inner Join ENT_ITEM_MASTER
On ENT_ITEM_MASTER.ITEM_KEY=ENT_LINK_OBJECTS.ENTITY_KEY And ENT_ITEM_MASTER.USER_LAST_MODIFIED Is Not Null) As B
输出:
OBJ_NAME ITEM_CODE ITEM_KEY
TDS-1980D-10+OP10+S7 TL-000032 1
TDS-1980D-10+OP10+S7 TL-000019 34
TDS-2258-01+OP10+S4 TL-000032 28
TDS-2258-01+OP10+S4 TL-000016 53
TDS-2325PU+OP10+S1 TL-000036 7
TDS-2325PU+OP10+S1 TL-000009 9
我已经在工作代码中加入了表格,这些表格给出了表A中表B中没有的所有内容。
我现在正在尝试右键加入表格,这些表格会给我表B中表格A中没有的所有内容。现在输出结果都没有。
正确加入的完整代码:
Select TDS, TL, IK
From (Select Sheet1.TOOLING_DATA_SHEET, Sheet1.CUTTING_TOOL, ENT_ITEM_MASTER.ITEM_KEY
From Sheet1
Inner Join ENT_ITEM_MASTER
On ENT_ITEM_MASTER.ITEM_CODE=Sheet1.CUTTING_TOOL And ENT_ITEM_MASTER.USER_LAST_MODIFIED Is Not Null) As A
Right Join (Select ENT_LINK_OBJECTS.OBJ_NAME As TDS, ENT_ITEM_MASTER.ITEM_CODE As TL, ENT_ITEM_MASTER.ITEM_KEY As IK
From ENT_LINK_OBJECTS
Inner Join ENT_ITEM_MASTER
On ENT_ITEM_MASTER.ITEM_KEY=ENT_LINK_OBJECTS.ENTITY_KEY And ENT_ITEM_MASTER.USER_LAST_MODIFIED Is Not Null) As B
On A.TOOLING_DATA_SHEET=B.TDS
Where A.TOOLING_DATA_SHEET is Null
当前输出:
TDS TL IK
所需的输出:( B中的所有不在A中的内容)
TDS TL IK
TDS-2258-01+OP10+S4 TL-000032 28
TDS-2325PU+OP10+S1 TL-000009 9
如果有帮助,我可以为我的左连接添加我的工作代码。我也尝试切换选择区域,执行左连接,并更改Null语句以尝试左连接,也不提供输出。
修改
我的代码如何使用左连接:
Select TDS, TL, IK
From (Select Sheet1.TOOLING_DATA_SHEET As TDS, Sheet1.CUTTING_TOOL As TL, ENT_ITEM_MASTER.ITEM_KEY As IK
From Sheet1
Inner Join ENT_ITEM_MASTER
On ENT_ITEM_MASTER.ITEM_CODE=Sheet1.CUTTING_TOOL And ENT_ITEM_MASTER.USER_LAST_MODIFIED Is Not Null) As A
Left Join (Select ENT_LINK_OBJECTS.OBJ_NAME, ENT_ITEM_MASTER.ITEM_CODE, ENT_ITEM_MASTER.ITEM_KEY
From ENT_LINK_OBJECTS
Inner Join ENT_ITEM_MASTER
On ENT_ITEM_MASTER.ITEM_KEY=ENT_LINK_OBJECTS.ENTITY_KEY And ENT_ITEM_MASTER.USER_LAST_MODIFIED Is Not Null) As B
On A.TDS=B.OBJ_NAME
Where B.OBJ_NAME is Null
输出:(A中的所有内容都不在B中)
TDS TL IK
TDS-1234-56-78 TL-000123 45
答案 0 :(得分:3)
---你有两个问题
On A.TDS=B.OBJ_NAME
and A.TL = B.Item_Code
and A.IK = B.Item_key
意味着最终结果将是:
Select B.TDS, B.TL, B.IK
From (Select Sheet1.TOOLING_DATA_SHEET, Sheet1.CUTTING_TOOL, ENT_ITEM_MASTER.ITEM_KEY
From Sheet1
Inner Join ENT_ITEM_MASTER
On ENT_ITEM_MASTER.ITEM_CODE=Sheet1.CUTTING_TOOL And ENT_ITEM_MASTER.USER_LAST_MODIFIED Is Not Null) As A
Right Join (Select ENT_LINK_OBJECTS.OBJ_NAME As TDS, ENT_ITEM_MASTER.ITEM_CODE As TL, ENT_ITEM_MASTER.ITEM_KEY As IK
From ENT_LINK_OBJECTS
Inner Join ENT_ITEM_MASTER
On ENT_ITEM_MASTER.ITEM_KEY=ENT_LINK_OBJECTS.ENTITY_KEY And ENT_ITEM_MASTER.USER_LAST_MODIFIED Is Not Null) As B
On A.TDS=B.OBJ_NAME
and A.TL = B.Item_Code
and A.IK = B.Item_ke
Where A.TOOLING_DATA_SHEET is Null
如果您的RDBMS支持MINUS(除了SQL SERVER),这也可以使用
(SELECT ENT_LINK_OBJECTS.OBJ_NAME As TDS,
ENT_ITEM_MASTER.ITEM_CODE As TL,
ENT_ITEM_MASTER.ITEM_KEY As IK
FROM ENT_LINK_OBJECTS
INNER JOIN ENT_ITEM_MASTER
ON ENT_ITEM_MASTER.ITEM_KEY=ENT_LINK_OBJECTS.ENTITY_KEY
AND ENT_ITEM_MASTER.USER_LAST_MODIFIED Is Not Null) As B
EXCEPT
(SELECT Sheet1.TOOLING_DATA_SHEET,
Sheet1.CUTTING_TOOL,
ENT_ITEM_MASTER.ITEM_KEY
FROM Sheet1
INNER JOIN ENT_ITEM_MASTER
ON ENT_ITEM_MASTER.ITEM_CODE=Sheet1.CUTTING_TOOL
AND ENT_ITEM_MASTER.USER_LAST_MODIFIED Is Not Null) A
它基本上是说取结果集B并从中减去结果集A.它会给你带来....你之后的两个记录..这只适用于所有列匹配的情况。外连接或存在/不存在提供了更大的灵活性。
关于加入Visual Aid以帮助更好地理解
答案 1 :(得分:2)
根据您在评论中描述的内容,您正在寻找一个不存在而不是正确的加入。 右连接将为您提供表B中的所有内容以及表A中匹配的所有内容。
不存在会在表B中找到表A中没有的所有内容。您需要类似下面的内容:
Select ENT_LINK_OBJECTS.OBJ_NAME, e1.ITEM_CODE, e1.ITEM_KEY
From ENT_LINK_OBJECTS
Inner Join ENT_ITEM_MASTER e1
On e1.ITEM_KEY=ENT_LINK_OBJECTS.ENTITY_KEY And e1.USER_LAST_MODIFIED Is Not Null
Where not exists
(Select *
From Sheet1
Inner Join ENT_ITEM_MASTER
On ENT_ITEM_MASTER.ITEM_CODE=Sheet1.CUTTING_TOOL And ENT_ITEM_MASTER.USER_LAST_MODIFIED Is Not Null
where ENT_ITEM_MASTER.ITEM_KEY = e1.ITEM_KEY)
答案 2 :(得分:0)
问题似乎很容易。你告诉我们:IT会给我表B中表A中没有的所有内容
但是在预期的结果中,你告诉我们TDS-2258-01 + OP10 + S4必须在输出中,并且这一行在表A上,所以它不会在输出中。
我认为您希望通过字段ITEM_KEY和IK进行连接,而不是使用A.TDS = B.OBJ_NAME
同样在输出中,您应该显示OBJ_NAME,ITEM_CODE和ITEM_KEY而不是TDS,TL,IK
这是最终查询:
Select OBJ_NAME, ITEM_CODE, ITEM_KEY
From (Select Sheet1.TOOLING_DATA_SHEET As TDS, Sheet1.CUTTING_TOOL As TL, ENT_ITEM_MASTER.ITEM_KEY As IK
From Sheet1
Inner Join ENT_ITEM_MASTER
On ENT_ITEM_MASTER.ITEM_CODE=Sheet1.CUTTING_TOOL And ENT_ITEM_MASTER.USER_LAST_MODIFIED Is Not Null) As A
Right Join (Select ENT_LINK_OBJECTS.OBJ_NAME, ENT_ITEM_MASTER.ITEM_CODE, ENT_ITEM_MASTER.ITEM_KEY
From ENT_LINK_OBJECTS
Inner Join ENT_ITEM_MASTER
On ENT_ITEM_MASTER.ITEM_KEY=ENT_LINK_OBJECTS.ENTITY_KEY And ENT_ITEM_MASTER.USER_LAST_MODIFIED Is Not Null) As B
On A.IK=B.ITEM_KEY
Where A.TDS is Null