我正在尝试在下表中执行查询(使用Access 2010):
Contact | Facility_Contact | Facility | Bank
| | |
ID | (fk) Contact_ID | ID | ID
CName | (fk) Facility_ID | FName | BName
| | BNumber | BNumber
我希望显示最终结果:
CName | FName | BName
以下是查询:
SELECT
HUD.[HOLDER NAME], Facility.PROVNAME, Contact.LAST_NAME
FROM
Facility INNER JOIN Bank ON Facility.[BNumber] = Bank.[BNumber]) INNER JOIN
(Contact INNER JOIN Facility_Contact ON Contact.[ID] = Facility_Contact.[Contact_ID]) ON Facility.[ID] = Facility_Contact.[Facility_ID];
这不会产生任何结果。
添加“银行”表时出现问题。从联系到工厂的查询工作,以及从工厂到银行的查询。但是,当我尝试从联系人链接到银行时,我很难产生结果。
在某处可能是Where-like子句?但现在我说的是我不太熟悉的事情;)
谢谢大家!
答案 0 :(得分:0)
FROM
Facility INNER JOIN Bank ON Facility.[BNumber] = HUD.[BNumber])
你关闭了一个paranthetical但从未打开它。虽然这会导致错误,所以我认为这是一个错字。
Facility INNER JOIN Bank ON Facility.[BNumber] = HUD.[BNumber]
您正在使用Facility
加入Bank
,但在您的ON条款中,您声明Facility.[BNumber] = HUD.[BNumber]
HUD来自哪里?这不好。
Facility INNER JOIN Bank ON Facility.[BNumber] = HUD.[BNumber]) INNER JOIN (Contact INNER JOIN Facility_Contact ON Contact.[ID] = Facility_Contact.[Contact_ID]) ON Facility.[ID] = Facility_Contact.[Facility_ID];
您的架构似乎是Facility-> Bank; Facility-> Facility_contact->联系。考虑重写以反映这一点(这只是为了可读性):
FROM
Facility
INNER JOIN Bank ON Facility.[BNumber] = Bank.[BNumber]
INNER JOIN Facility_Contact ON Facility.[ID] = Facility_Contact.[Facility_ID]
INNER JOIN Contact ON Facility_Contact.[Contact_ID] = Contact.[ID]
现在要获取记录,所有这些连接都需要包含数据。对于BNUMBER
表格中的每个Bank
,您的BNumber
表格中需要有匹配的Facility
。您facility_id
中Facility_Contact
表中的每个ID
都需要匹配Facility
。您的ID
表中的每个Contact
都需要在Contact_ID
表格中匹配Facility_Contact
。如果缺少任何这些链接,您将获得0条记录。如果您想显示Facilties和银行名称,无论他们是否有设施联系人,请考虑使用LEFT JOIN:
FROM
Facility
INNER JOIN Bank ON Facility.[BNumber] = Bank.[BNumber]
LEFT JOIN Facility_Contact ON Facility.[ID] = Facility_Contact.[Facility_ID]
LEFT JOIN Contact ON Contact.[ID] = Facility_Contact.[Contact_ID]
这不会因为您在设施中没有联系人而导致结果丢失。或者联系人表中尚未设置该联系人。