Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PostgreSQL-帮助将两个表与三个特定列进行比较_Sql_Database_Postgresql - Fatal编程技术网

PostgreSQL-帮助将两个表与三个特定列进行比较

PostgreSQL-帮助将两个表与三个特定列进行比较,sql,database,postgresql,Sql,Database,Postgresql,我需要在两个表中匹配三列。查询应选择t1中的一行,并搜索t2中下面列出的所有三列都匹配的任何行 tbl_staged_documentation (t1 for reference) orgname|name|subnet|customerid|customername|ipaddress|prefix tbl_active_networks (t2 for reference) orgid|ipaddress|prefixlength|sitename|siteid|name 这是三列 t

我需要在两个表中匹配三列。查询应选择t1中的一行,并搜索t2中下面列出的所有三列都匹配的任何行

tbl_staged_documentation (t1 for reference)
orgname|name|subnet|customerid|customername|ipaddress|prefix

tbl_active_networks (t2 for reference)
orgid|ipaddress|prefixlength|sitename|siteid|name
这是三列

t1.customerid = t2.orgid
t1.ipaddress = t2.ipaddress
t1.prefix = t2.prefixlength
我已经调查过一个联合会。工会看起来可以复制,但我没能得到。也没有加入

似乎这两个选项中的一个是可行的,但我不清楚如何做到这一点

select *
from tbl_staged_documentation t1
join tbl_active_networks t2
  on t1.customerid = t2.orgid
  and t1.ipaddress = t2.ipaddress
  and t1.prefix = t2.prefixlength
where
  t1.customerid = t2.orgid AND t1.ipaddress != t2.ipaddress AND t1.prefix != t2.prefixLength;
还尝试了以下联盟

select customerid, ipaddress, prefix from tbl_staged_documentation
union
select orgid, ipaddress, prefixlength from tbl_active_networks;
最后,我试图找出t1中哪些网络信息在t2中不存在。t1是真理的源泉。t2包含生产系统中的数据


t1中的数据将动态更新到t2中,但由于t2数据来自的系统上存在严重的速率限制,我正在尝试在运行API调用之前对其进行清理。

不是100%确定您的问题,但我认为您可能需要的是“反加入”,如:

即:

  • 使用匹配条件:

    t1.customerid = t2.orgid
    t1.ipaddress = t2.ipaddress
    t1.prefix = t2.prefixlength
    
  • 查找
    t1
    中哪些行在
    t2
    中没有任何匹配行

我试图找出t1中哪些网络信息在t2中不存在

这正是NOT EXISTS操作符的作用:

select *
from tbl_staged_documentation t1
where not exists (select *
                  from tbl_active_networks t2
                  where t1.customerid = t2.orgid
                    and t1.ipaddress = t2.ipaddress
                    and t1.prefix = t2.prefixlength)

@没有名字的马应该是对的

此处有更多关于包含(不存在)和(不存在)的信息

select *
from tbl_staged_documentation t1
where not exists (select *
                  from tbl_active_networks t2
                  where t1.customerid = t2.orgid
                    and t1.ipaddress = t2.ipaddress
                    and t1.prefix = t2.prefixlength)