繁体   English   中英

比较同一表的两列中的字符串

[英]Comparing strings in two columns of the same table

我有以下数据-

Code1   Code2
G01621  E04621

我想显示所有在代码1中但不在代码2中的代码。

我编写了以下SQL,但返回的数据不正确-

select * from codes c1
left join codes c2 
on lower(c1.code1) = lower(c2.code2)
where c2.code2 is null

您可以使用not exists

select c.code1
from codes c
where not exists (select 1
                  from codes c2
                  where lower(c2.code2) = lower(c.code1)
                 );

使用trim()函数

select * from codes c1
left join codes c2 
on trim(lower(c1.code1)) = trim(lower(c2.code2))
where c2.code1 is null;

演示版

用途except

select c1.code1 from codes c1
except
select c2.code2 from codes c2

暂无
暂无

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

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