0

我正在尝试将多个表作为单个视图访问,但我的查询中没有匹配任何数据。我相当确定问题出在视图声明中。

CREATE VIEW IF NOT EXISTS method_view 
AS SELECT 
 peercreateddata.id,
 peercreateddata.name,
 peercreateddata.shared,
 peercreateddata.authorHash,
 peercreateddata.creationDate,
 peercreateddata.firstSeen,
 peercreateddata.lang,
 methods.primaryMatch,
 methods.methodHash,
 methods.methodbyline,
 methods.methodexpirationDate,
 methods.methodlocation,
 localResources.localmimeType,
 remoteResources.remotemimeType,
 remoteResources.remotelocation 

FROM peercreateddata peercreateddata,
 methods methods,
 localResources localResources,
 remoteResources remoteResources 

WHERE (methods.peerDataId = peercreateddata.id) 
      AND (localResources.localMethodId = methods.id) 
      AND (remoteResources.remoteMethodId = methods.id) ;

查看 WHERE 子句,methods.peerDataId 始终匹配 peercreateddata.id 的单个值。表 localResources 和 remoteResources 有时为空,有时不包含查询的匹配项,有时包含多个匹配项。

我正在为 Android 开发。该语法通过了一个验证器(尽管一些关于使用名为“名称”的字段的抱怨)。

4

1 回答 1

1

使用内部和外部联接子句。请参阅文档

用这个替换你的 from 子句并去掉 where

FROM peercreateddata peercreateddata
 inner join methods methods 
       on methods.peerDataId = peercreateddata.id
 left outer join localResources localResources 
       on localResources.localMethodId = methods.id
 left outer join remoteResources remoteResources 
       on  remoteResources.remoteMethodId = methods.id
于 2012-12-22T14:01:49.620 回答