【问题标题】:Postgresql, Copy data to a new table from a foreign tablePostgresql,将数据从外部表复制到新表
【发布时间】:2016-12-28 21:03:20
【问题描述】:

我正在尝试将数据从架构“nathalia”的名为“m_aduana”的外部表复制到我的架构“publico”和我的表“mae_aduana”。

我需要做一个查询,复制表“m_aduana”中的所有值,避免重复。

我现在得到了类似的东西,但结果给我发送了一个 Insert 0 0,这意味着没有插入任何内容。

insert into publico.mae_aduana(cod_aduana,nom_aduana,des_aduana,cod_aduana1,cod_aduana2,cod_aduana3,est_aduana)
select cod_aduana,nom_aduana,des_aduana,cod_aduana1,cod_aduana2,cod_aduana3,est_aduana 
    from nathalia.m_aduana
    where not exists (
    select * from publico.mae_aduana ma_ad, nathalia.m_aduana m_ad
        where ma_ad.cod_aduana = m_ad.cod_aduana)

【问题讨论】:

  • 更有效的方法是使用insert ... on conflict do nothing 而不是where not exists

标签: sql postgresql foreign-data-wrapper


【解决方案1】:

我认为您在内部选择中有错误。您无需再次使用表 nathalia.m_aduana。如果应该是这样的:

insert into publico.mae_aduana(cod_aduana,nom_aduana,des_aduana,cod_aduana1,cod_aduana2,cod_aduana3,est_aduana)
select cod_aduana,nom_aduana,des_aduana,cod_aduana1,cod_aduana2,cod_aduana3,est_aduana 
    from nathalia.m_aduana
    where not exists (
    select * from publico.mae_aduana ma_ad
        where ma_ad.cod_aduana = nathalia.m_aduana.cod_aduana)

【讨论】:

  • 太棒了!有用。谢谢,就像你说的,我的问题在参考上。
【解决方案2】:

您可能想要更改where exists 部分,如下所示

from nathalia.m_aduana m
where not exists (
select 1 from publico.mae_aduana
    where cod_aduana = m.cod_aduana)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多