我有两个查找表,我希望彼此内部联接,但我只想在数据表上保持连接。
select * from CLIENT
LEFT JOIN [ENTRY]
on [ENTRY].ENTRY_CODE = CLIENT.CLIENT_COUNTY
and [ENTRY].ENTRY_RECD = 'A'
INNER JOIN [ENTRY_TYPES]
on [ENTRY_TYPES].ENTRY_TYPES_FileRecordID = [ENTRY].ENTRY_TYPE
and [ENTRY_TYPES].ENTRY_TYPES_CODE = 'COUNTY'
and [ENTRY_TYPES].ENTRY_TYPES_RECD = 'A'
where CLIENT_RECD = 'A'
因此,如果ENTRY_TYPES_FileRecordID = ENTRY_TYPE
失败,我不希望该ENTRY
条记录可用作加入CLIENT
的左连接项。
编写上述代码的方式是ENTRY
上的左连接表现得像内连接。
答案 0 :(得分:0)
当我输入问题时,我自己想出来,我需要将内部联接分离出来作为子查询
select * from CLIENT
left join
(
select ENTRY_CODE, ENTRY_NAME
from [ENTRY]
inner join [ENTRY_TYPES] on ENTRY_TYPES_FileRecordID = ENTRY_TABLE
and ENTRY_TYPES_CODE = 'COUNTY'
and ENTRY_TYPES_RECD = 'A'
where ENTRY_RECD = 'A'
) as CountyLookup on CLIENT_COUNTY = ENTRY_CODE
where CLIENT_RECD = 'A'
答案 1 :(得分:0)
您已经找到了解决方案,但现在您已将其设为子查询。您也可以通过在原始查询中移动一些部分并添加两个括号来完成此操作。
所以这只是为了展示另一种选择:
SELECT *
FROM [CLIENT]
LEFT JOIN ([ENTRY] --opening bracket
INNER JOIN [ENTRY_TYPES]
ON [ENTRY_TYPES].[ENTRY_TYPES_FileRecordID] = [ENTRY].[ENTRY_TYPE]
AND [ENTRY_TYPES].[ENTRY_TYPES_CODE] = 'COUNTY'
AND [ENTRY_TYPES].[ENTRY_TYPES_RECD] = 'A') -- closing bracket
ON [ENTRY].[ENTRY_CODE] = [CLIENT].[CLIENT_COUNTY]
AND [ENTRY].[ENTRY_RECD] = 'A'
WHERE [CLIENT_RECD] = 'A'