我有两个信息表。一个存在于系统中,另一个正在导入以比较和更新信息。我正在尝试使用右连接来获取表B中不在表A中的信息。我需要从表B中引入信息,因此输出值不是NULL。
完整代码:
Select TDS, TL, IK
From (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) As A
RIGHT JOIN Sheet1 B
On A.TDS=B.TOOLING_DATA_SHEET
Where A.TDS is Null
示例:
表A
来自代码:
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
应该产生:
OBJ_NAME ITEM_CODE IK
tds-123 tl-1234 387
tds-456 tl-8721 879
tds-983 tl-1987 928
表B 使用代码:
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
应该产生:
TOOLING_DATA_SHEET CUTTING_TOOL ITEM_KEY
tds-123 tl-1234 387
tds-456 tl-8721 879
tds-983 tl-1987 928
tds-873 tl-9873 827
tds-982 tl-9872 287
目前提供的代码:
TDS TL IK
NULL NULL NULL
NULL NULL NULL
代码应该回馈的内容:
TDS TL IK
tds-873 tl-9873 827
tds-982 tl-9872 287
左下角编辑尝试 - 无法正常工作,需要实施Sheet1的加入
Select l.OBJ_NAME As TDS, i.ITEM_CODE As TL, i.ITEM_KEY As IK
From ENT_LINK_OBJECTS l
Join ENT_ITEM_MASTER i On i.ITEM_KEY=l.ENTITY_KEY
Left Join Sheet1 s on l.OBJ_NAME = s.TOOLING_DATA_SHEET and
i.ITEM_CODE = s.CUTTING_TOOL and
i.ITEM_KEY = i.ITEM_KEY
where s.CUTTING_TOOL is null
答案 0 :(得分:1)
您可以使用left join
/ is null
:
select l.OBJ_NAME As TDS
, i.ITEM_CODE As TL
, i.ITEM_KEY As IK
from ENT_LINK_OBJECTS l
join ENT_ITEM_MASTER i on i.ITEM_KEY = l.ENTITY_KEY
left Join Sheet1 s on l.OBJ_NAME = s.TOOLING_DATA_SHEET and
i.ITEM_CODE = s.CUTTING_TOOL and
i.ITEM_KEY = s.ITEM_KEY
where s.CUTTING_TOOL is null
答案 1 :(得分:0)
尝试此查询
select a.* from tableb a
right join tablea b on a.tooling_data_sheet=b.obj_name
where b.obj_name is null
答案 2 :(得分:0)
您也可以考虑使用not exists
:
select lo.*
From ENT_LINK_OBJECTS lo
where not exists (select 1
from ENT_ITEM_MASTER im
where im.ITEM_KEY = lo.ENTITY_KEY
);
缩写lo
和im
称为表别名。使用它们是个好主意。有时他们是需要的;更常见的是,它们只是使查询更容易编写和阅读。对它们使用表名缩写是最佳实践。
答案 3 :(得分:0)
解决方案:当使用具有多个内部联接语句的左连接时,您可以定义如下
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) 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) As B
On A.TDS=B.OBJ_NAME
Where B.OBJ_NAME is Null