繁体   English   中英

连接同一表的两列并比较结果

[英]Connecting two columns of the same table and comparing results

我有表CSBaumkontakt ,这是一个CSBaumkontakt查询:

select rootpubindex
from csbaumkontakt
where dadpubindex = '2143640C-3ABF-440C-B094-2128F4495874'

结果为rootpubindex = F7BC36A0-AD4F-49D5-B4FC-AD529C7EA738

select rootpubindex, dadpubindex
from csbaumkontakt
where relpubindex = '2143640C-3ABF-440C-B094-2128F4495874'

结果rootpubindex = D66035CB-B7AD-4D54-9FC7-AE6E7F32662Ddadpubindex = D66035CB-B7AD-4D54-9FC7-AE6E7F32662D

Dadpubindex第一次查询的是relpubindex第二查询。

现在我想知道第二个查询中的rootpubindex = D66035CB-B7AD-4D54-9FC7-AE6E7F32662Ddadpubindex = D66035CB-B7AD-4D54-9FC7-AE6E7F32662D与第二个查询中的rootpubindex = F7BC36A0-AD4F-49D5-B4FC-AD529C7EA738第一个查询。 因此在这个例子中没有。 我可以通过一个查询查看表中的每一行是否相同?

似乎您正在寻找两列中具有相同值的行

select rootpubindex 
     , dadpubindex 
from csbaumkontakt 
where dadpubindex = relpubindex

的,以查看比赛结果

select rootpubindex 
     , dadpubindex 
     , adpubindex = relpubindex  /* 0 = not equal, 1 = equal */
from csbaumkontakt 

您想知道是否存在相关记录( rel.relpubindex = main.dadpubindex ),且相关记录的rootpubindexdadpubindex与主记录的rootpubindex相匹配。 为此使用EXISTS

select
  dadpubindex,
  rootpubindex,
  case when exists 
  (
    select * 
    from csbaumkontakt rel 
    where rel.relpubindex  = main.dadpubindex
      and rel.rootpubindex = main.rootpubindex
      and rel.dadpubindex  = main.rootpubindex
  ) then 'yes' else 'no' end as match_exists
from csbaumkontakt main;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM