Oracle 如何比较表中同一行下的多个列?

Oracle 如何比较表中同一行下的多个列?,oracle,plsql,Oracle,Plsql,在my where子句中,表的以下列不应相等 cd_交付地址 cd\邮件\递送\地址 cd_st_代码 cd\U邮件\U st\U代码 cd_-zip cd\u邮件\u邮政编码 请查找我的代码片段以实现此目的: select * from table cd where ( (cd_mail_delivery_address <> cd_delivery_address or (cd_mail_delivery_address is null and c

在my where子句中,表的以下列不应相等

  • cd_交付地址
  • cd\邮件\递送\地址
  • cd_st_代码
  • cd\U邮件\U st\U代码
  • cd_-zip
  • cd\u邮件\u邮政编码
请查找我的代码片段以实现此目的:

     select * from table cd
     where
(
(cd_mail_delivery_address <> cd_delivery_address or    
(cd_mail_delivery_address is null and cd_delivery_address is not null) or
(cd_mail_delivery_address is not null and cd_delivery_address is null)
)
and (
cd.cd_city <> cd.cd_mail_city or 
(cd.cd_city is null and cd_mail_city is not null) or 
(cd_city is not null and cd_mail_city is null))
and (
cd.st_code <> cd.cd_mail_st_code or 
(cd.st_code is null and cd_mail_st_code is not null) or 
(st_code is not null and cd_mail_st_code is null)
)
and (
cd.cd_zip <> cd.cd_mail_zip or 
(cd.cd_zip is null and cd_mail_zip is not null) or 
(cd_zip is not null and cd_mail_zip is null)
)
)
从表cd中选择*
哪里
(
(cd\邮件\递送\地址cd\递送\地址或
(cd\ U邮件\交付\地址为空且cd\ U交付\地址不为空)或
(cd\邮件\传递\地址不为空,cd\传递\地址为空)
)
及(
cd.cd\城市cd.cd\邮件\城市或
(cd.cd_city为空,cd_mail_city不为空)或
(cd_city不为空,cd_mail_city为空)
及(
cd.st_代码cd.cd_邮件\u st_代码或
(cd.st_代码为空,cd_邮件st_代码不为空)或
(st_代码不为空,cd_邮件st_代码为空)
)
及(
cd.cd\u zip cd.cd\u邮件\u zip或
(cd.cd_-zip为空,cd_-mail_-zip不为空)或
(cd_-zip不为空,cd_-mail_-zip为空)
)
)

所有列都是varchar2,我得到了该代码的正确输出。但是在pl sql中比较多个列是更好的方法吗?我可以改进这个代码吗?任何建议都会有帮助。

您可以用NVL函数替换空检查,如下所示:

...
NVL(cd_mail_delivery_address,'_') <> NVL(cd_delivery_address,'_')
...
。。。
NVL(cd邮件递送地址)NVL(cd递送地址)
...

它确实更具可读性,但我不确定查询效率

我使用连接对两列进行了测试:

select a.cd_delivery_address,b.cd_mail_delivery_address 
from cd a inner join cd b 
where a.cd_delivery_address <> b.cd_mail_delivery_address and
a.cd_delivery_address = b.cd_delivery_address
选择a.cd\u传送地址、b.cd\u邮件\u传送地址
从cd a内部连接cd b
其中a.cd\u递送地址b.cd\u邮件\u递送地址和
a、 cd_交货地址=b.cd_交货地址

在这里,空检查条件将被省略,并将减少条件的数量,但由于涉及到联接,因此会对性能产生影响。

为什么需要存储过程?