【发布时间】:2011-04-12 11:35:42
【问题描述】:
访问数据库
table contacts
--------------
id
surname
name
table relations
---------------
contact_id
relation_id
contact_id 和relation_id 都是引用表contacts id 的外键
我想执行查询以获取联系人的姓/名和关系的姓/名如果当前联系人的关系存在。如果它不存在,我想获取联系人的姓/名和关系字段的空白值。
所有这些都在一个查询中
编辑:
我使用左连接。我正在使用 VB.NET 运行查询:
Dim myOleDbDataReader As OleDbDataReader = _
New OleDbCommand( _
"SELECT c.id AS contact_id " & _
" , c.surname AS contact_surname " & _
" , c.name AS contact_name " & _
" , c2.id AS related_id " & _
" , c2.surname AS related_surname " & _
" , c2.name AS related_name " & _
"FROM ((contacts c " & _
"LEFT JOIN relations r " & _
"ON c.id = r.contact_id) " & _
"INNER JOIN contacts c2 " & _
"ON c2.id = r.relation_id)" _
, connection).ExecuteReader()
我得到 OleDbException: Join expression not supported.
他们在另一篇文章中说: “当您在 FROM 子句中使用 LEFT/RIGHT/INNER JOINS 时,Access 不允许您在 where 子句中使用常规连接。这可能是故意让您购买更昂贵的软件。” - (Is the join expression not supported by MS Access?)
不完全是这样。从我尝试的一些示例中,我得出的结论是:
Access 不允许您将外部联接(左/右)与一个或多个内部联接一起使用。 以约翰·卡马克的名义,我能做什么? 我想避免单独的选择查询。 请帮忙...
【问题讨论】:
标签: sql ms-access join database-relations