如何比较sqlserver中的两个表

relj7zay  于 2021-08-13  发布在  Java
关注(0)|答案(1)|浏览(362)

**结束。**此问题不符合堆栈溢出准则。它目前不接受答案。
**想改进这个问题吗?**更新问题,使其成为堆栈溢出的主题。

5个月前关门了。
改进这个问题
我需要比较SQLServer中的两个表,如下所示,并且需要比较两列。
表1

SystemName, ReconID

hostname1,Recon1
hostname2,Recon2
hostname7,Recon3

表2

AssetName, ReconID

hostname1,Recon1
hostname2,Recon6
hostname3,Recon3

比较条件:
systemname=assetname和reconid<>reconid
systemname<>assetname和reconid=reconid
systemname=assetname和reconid=reconid
请帮助我进行sql查询。我试过下面的东西。

jgwigjjp

jgwigjjp1#

如果要查看数据,可以执行以下操作:

select "Condition 1" as condition
     , SystemName
     , AssetName
     , Table1.ReconID as "Table1_ReconID"
     , Table2.ReconID as "Table2_ReconID"
from Table1, Table2
where SystemName = AssetName AND Table1.ReconID <> Table2.ReconID
union 
select "Condition 2" as condition
     , SystemName
     , AssetName
     , Table1.ReconID as "Table1_ReconID"
     , Table2.ReconID as "Table2_ReconID"
from Table1, Table2
where SystemName <> AssetName AND Table1.ReconID = Table2.ReconID
union
select "Condition 3" as condition
     , SystemName
     , AssetName
     , Table1.ReconID as "Table1_ReconID"
     , Table2.ReconID as "Table2_ReconID"
from Table1, Table2
where SystemName = AssetName AND Table1.ReconID = Table2.ReconID
order by condition

在交叉的情况下,使用“union all”而不是“union”。

相关问题