0

我有两个查找表,我想内部连接,但是我只想在我的数据表上左连接。

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就像一个内连接。

4

2 回答 2

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'
于 2013-02-14T18:56:58.730 回答
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'
于 2013-02-14T19:16:45.953 回答