MS Access 2002 / VBA - 将多个查找表连接到主查询?

时间:2014-08-18 07:19:14

标签: sql vba ms-access

考虑:

contact
=======
contact_id, not null
name, not null
electorate_id, null allowed
member_type_id, null allowed

member_types
===========+
member_type_id
member_type

electorates
===========
electorate_id
electorate

这些是我希望用来填充表单的表。我尝试了几种方法,但查询没有返回数据:

SELECT contact_id, name, member_type, electorate
FROM contact LEFT JOIN member_types 
ON contacts.member_type_id=member_types.member_type_id,
contacts LEFT JOIN electorates
ON contacts.electorate_id=electorates.electorate_id

此外:

"SELECT contact_id, name, member_types.member_type, electorates.electorate
FROM contacts, member_types, electorates
WHERE contacts.contact_id=" & contact_id & " 
and electorates.electorate_id=contacts.electorate_id
and member_types.member_type_id = contacts.member_type_id"

这两个都失败了。有人可以建议一个有效的查询吗?

1 个答案:

答案 0 :(得分:1)

SELECT contact_id, name, member_type, electorate
FROM contact 
LEFT JOIN member_types ON contact.member_type_id=member_types.member_type_id
LEFT JOIN electorates ON contact.electorate_id=electorates.electorate_id

请勿在第4行中重新指定联系表。此外,您在ON条款中有联系人,而不是联系

希望ACCESS 2002支持LEFT JOINS。

此外,您的第二个查询仅在设置了electorate_id和member_id时才有效。这就是你需要LEFT JOIN的原因。