【问题标题】:PostgreSQL: Column "crab_id" Specified More than OncePostgreSQL:列“crab_id”指定了不止一次
【发布时间】:2015-11-12 16:49:54
【问题描述】:

使用 PostgreSQL 调用,

CREATE TABLE condor_xrootd AS
       SELECT * FROM condor INNER JOIN xrootd_ext
       ON (
xrootd_ext.CRAB_Id = condor.CRAB_Id AND
REPLACE(condor.CRAB_ReqName, '_', ':') = xrootd_ext.CRAB_ReqName
);

我得到了错误,

$ psql condor -f ./sql/inner_join.sql 
psql:./sql/inner_join.sql:6: ERROR:  column "crab_id" specified more than once

这是可以理解的,因为每个表都有一个 Crab_Id 列。我希望能够在不必指定列的情况下进行内部连接,因为我在两个表中总共有大约 400 列。

请告诉我是否可以在不单独列出列的情况下以某种方式消除此错误。

编辑:

我忘了提到速度和稳定性在这里很重要,因为我的加入可能需要几天时间。

【问题讨论】:

  • 对于连接,您可以在连接具有相同名称的列时尝试使用 USING 子句而不是 ON,例如condor INNER JOIN xrootd_ext 使用(CRAB_Id)。使用它时,生成的连接应该只有一个被连接的列的实例。在 PostreSQL 中可能有效,也可能无效,但您可以试一试。
  • 看看这个问题:stackoverflow.com/questions/15061733/…我相信它回答了你的问题。

标签: postgresql inner-join


【解决方案1】:
create table condor_xrootd as
select *
from
    condor
    inner join
    xrootd_ext using (crab_id)
where replace(condor.crab_reqname, '_', ':') = xrootd_ext.crab_reqname

【讨论】:

  • 你知道你的答案是否会比 stas.yaranov 的更快吗?
  • @aidan 大概一样
【解决方案2】:

您从由两个表构建的结果集中创建表。您的错误表明 crab_id 列同时存在于 condorxrootd_ext 表中,并且 Postgresql 不知道应该选择哪一个。

您应该指定要创建表的字段。像这样:

CREATE TABLE condor_xrootd AS
       SELECT
          condor.*, -- here we take all fields from condor table
          xrootd_ext.crab_reqName -- here we take crab_reqName field from xrootd_ext
       FROM condor INNER JOIN xrootd_ext
       ON (
xrootd_ext.CRAB_Id = condor.CRAB_Id AND
REPLACE(condor.CRAB_ReqName, '_', ':') = xrootd_ext.CRAB_ReqName
);

【讨论】:

  • Clodoaldo Neto 的答案似乎有效并且更简单,因为我想要所有 XRootD 列。我担心 WHERE 子句。你知道哪个更快和/或你能证明吗?
  • 不应该有任何性能差异。我认为 EXPLAIN 将证明这一点。所以用对你更好的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-13
  • 2011-08-10
  • 2015-06-03
相关资源
最近更新 更多