将预连接表与另一个表组合

时间:2012-01-07 00:28:36

标签: sql postgresql join inner-join union-all

我正在尝试将一个预先连接的表与另一个表并排加入,但它似乎无法正常工作。

以下是代码:

SELECT
  r.domainid,
  r.dombegin AS DomainStart,
  r.domend AS Domain_End,
  d.ddid,
  d.confid1 AS confid,
  c.pdbcode,
  c.chainid,
  a.pdbcode AS "cath_pdbcode", 
  c.pdbcode
FROM dyn_dyndomrun d, cath_domains a
  INNER JOIN dyn_conformer c ON d.confid1 = c.id
  INNER JOIN dyn_domainregion r ON r.domainid::varchar(8) = d.ddid
  INNER JOIN dyn_conformer AS c ON a.pdbcode::character(4) = c.pdbcode
UNION ALL
SELECT
  NULL,
  NULL,
  NULL,
  NULL,
  NULL,
  d.ddid,
  d.confid2,
  c.pdbcode,
  c.chainid
FROM dyn_dyndomrun d 
  INNER JOIN dyn_conformer c ON d.confid2 = c.id
  ORDER BY confid ASC

此行有问题

FROM dyn_dyndomrun d, cath_domains a
      INNER JOIN dyn_conformer c ON d.confid1 = c.id
      INNER JOIN dyn_domainregion r ON r.domainid::varchar(8) = d.ddid
      INNER JOIN dyn_conformer AS c ON a.pdbcode::character(4) = c.pdbcode

这是错误:

ERROR:  invalid reference to FROM-clause entry for table "d"
LINE 11:   INNER JOIN dyn_conformer c ON d.confid1 = c.id
                                         ^
HINT:  There is an entry for table "d", but it cannot be referenced from this part of the query.


********** Error **********

ERROR: invalid reference to FROM-clause entry for table "d"
SQL state: 42P01
Hint: There is an entry for table "d", but it cannot be referenced from this part of the query.
Character: 236

最后,我希望有一个表格,"domainid, domainstart, domainend, ddid, confid, chainid, pdbcode from conformer and the chain id",并且我希望从另一个表中有一组新的列,例如"pdbcode from cath_domains, cathbegin, cathend"

conformer和cath_domains的pdbcode相互匹配,因此我想交叉引用它们。

我做错了吗?

1 个答案:

答案 0 :(得分:2)

停止执行隐式交叉连接。同样正如ypercube指出的那样,还需要清理一些其他小东西,特别是删除重复的连接。

SELECT
  r.domainid,
  r.dombegin AS DomainStart,
  r.domend AS Domain_End,
  d.ddid,
  d.confid1 AS confid,
  c.pdbcode,
  c.chainid,
  a.pdbcode AS "cath_pdbcode", 
  c.pdbcode
FROM dyn_dyndomrun d
  INNER JOIN dyn_conformer c ON d.confid1 = c.id 
  INNER JOIN dyn_domainregion r ON r.domainid::varchar(8) = d.ddid
  INNER JOIN cath_domains a ON a.pdbcode::character(4) = c.pdbcode
UNION ALL
SELECT
  NULL,
  NULL,
  NULL,
  NULL,
  NULL,
  d.ddid,
  d.confid2,
  c.pdbcode,
  c.chainid
FROM dyn_dyndomrun d 
  INNER JOIN dyn_conformer c ON d.confid2 = c.id
  ORDER BY confid ASC