通过视图访问多个表

时间:2012-12-22 13:54:32

标签: sqlite

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

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开发。语法传递验证器(尽管有人抱怨使用名为'name'的字段)。

1 个答案:

答案 0 :(得分:1)

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

用这个替换你的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