null LIKE'%text%'?

时间:2015-10-20 04:09:33

标签: sql oracle null sql-like

由于条件add si, cx add si, cx ,该字段包含not like '%TEST%'值,因此我遗漏了很多记录。

NULL

select * from credit_case cc left join (select skp_case, name_full from client) cl on cc.skp_case = cl.skp_case where cl.name_full not like '%TEST%' 包含完整数据,而表credit_case则不包含。

当我将其重新编写为

client
来自select * from credit_case cc left join (select skp_case, name_full from client where name_full not like '%TEST%') cl on cc.skp_case = cl.skp_case

记录不会丢失。 为什么?

4 个答案:

答案 0 :(得分:3)

  • null永远不会等于另一个值,包括null
  • null永远不会与其他值不相等,包括null
  • null永远不会像其他值,包括null
  • null永远不会与其他值不同,包括null

null进行比较的唯一方法是使用is nullis not null。这些查询都不会返回一行。

select *
  from table
 where column = null;

select *
  from table
 where column != null;

select *
  from table
 where column like null;

select *
  from table
 where column not like null;

您需要明确包含is nullis not null子句

where (   cl.name_full not like '%TEST%'
       or cl.name_full is null)

将返回null的{​​{1}}值的行。

答案 1 :(得分:0)

在第一种情况下,有一个条件子句将从结果中过滤掉一些记录。

在第二种情况下,主表上没有条件子句。你正在做的事实上是

select * 
from credit_case cc
left join [SUBTABLE] 

这样肯定会为您提供主表credit_case

的完整列表

答案 2 :(得分:0)

在第一种情况下,左连接返回所有行,然后where子句因Nulls而过滤掉行

在第二种情况下,where子句由于空值而过滤掉行。然后左连接将它们添加回来。 如果运行EXPLAIN PLAN,您可以看到操作的顺序,是否首先发生由于null的过滤或者由于左连接而发生的包含

答案 3 :(得分:0)

当您评估NULL AND或OR Condition时,结果始终为false。

select * 
from credit_case cc
left join (select skp_case, name_full from client) cl on cc.skp_case = cl.skp_case
where cl.name_full not like '%TEST%'

如果credit_case中没有记录,则客户端的联接会导致cl.name_full的结果为NULLS。

试试这个,你会理解:

select cl.name_full
from credit_case cc
left join (select skp_case, name_full from client) cl on cc.skp_case = cl.skp_case

所有NULL记录都是从查询结果中省略的记录。

另一方面,对于第二种情况,当你重写为

select * 
from credit_case cc
left join (select skp_case, name_full from client 
           where name_full not like '%TEST%') cl on cc.skp_case = cl.skp_case

这里假设name_full不是NULL,所有记录都没有'%TEST%'在name_full中显示。并且不会使用'%TEST%'来评估NULL这里。