SQL Server-左外部联接在两个表之一上

问题描述 投票:-1回答:1

我有2个主表AGENCYPERSONEL,我可以从中选择:

-- two primary tables -- works well
select 
    -- AT_AGENCY_STAFF.PERS_SSN as PERS_SSN 
    an_agency_table.U_AGENCY_NUMBER as AGENCY_NUMBER,
    an_personel_id_table.U_PERSONNEL_ID as PERSONNEL_ID  
from 
    [dbo].[an_agency_table],
    [dbo].[an_personel_id_table]
 -- left outer join [dbo].[AT_AGENCY_STAFF] on AT_AGENCY_STAFF.id_an_agency_table = [dbo].[an_agency_table].id_an_agency_table
 where 
     [dbo].[an_personel_id_table].[U_PERSONNEL_ID] = 1 
     and [dbo].[an_agency_table].[U_AGENCY_NUMBER] = '777778' 

当仅使用一个主表时,我可以通过左外部联接(如果没有人员,则可以连接AGENCY_STAFF)>

-- one primary table and left outer join -- works well
select 
AT_AGENCY_STAFF.PERS_SSN as PERS_SSN 
, an_agency_table.U_AGENCY_NUMBER as AGENCY_NUMBER 
-- , an_personel_id_table.U_PERSONNEL_ID as PERSONNEL_ID  
 from 
 [dbo].[an_agency_table]
-- ,[dbo].[an_personel_id_table]
 left outer join [dbo].[AT_AGENCY_STAFF] on AT_AGENCY_STAFF.id_an_agency_table = [dbo].[an_agency_table].id_an_agency_table
 where -- [dbo].[an_personel_id_table].[U_PERSONNEL_ID]=1 and
  [dbo].[an_agency_table].[U_AGENCY_NUMBER]='777778' 

但是当我同时使用两个主表并被遗漏连接到其中一个时,我会报错:

-- two primary tables and left outer join -- Msg 4104, Level 16, State 1, Line 9 -- The multi-part identifier "dbo.an_agency_table.id_an_agency_table" could not be bound.
select 
AT_AGENCY_STAFF.PERS_SSN as PERS_SSN 
, an_agency_table.U_AGENCY_NUMBER as AGENCY_NUMBER 
, an_personel_id_table.U_PERSONNEL_ID as PERSONNEL_ID  
 from 
 [dbo].[an_agency_table]
 ,[dbo].[an_personel_id_table]
 left outer join [dbo].[AT_AGENCY_STAFF] on AT_AGENCY_STAFF.id_an_agency_table = [dbo].[an_agency_table].id_an_agency_table
 where -- [dbo].[an_personel_id_table].[U_PERSONNEL_ID]=1 and
  [dbo].[an_agency_table].[U_AGENCY_NUMBER]='777778' 

有关如何解决此问题的任何建议?

我有2个主表AGENCY和PERSONEL,我可以从中选择:-两个主表-可以很好地选择-AT_AGENCY_STAFF.PERS_SSN作为PERS_SSN an_agency_table.U_AGENCY_NUMBER作为...

sql-server left-join
1个回答
0
投票

请尝试使用下面的格式,不过我对您的命名约定没有过多的注意。

© www.soinside.com 2019 - 2024. All rights reserved.