SqlServer_查询带有null的记录

字段的三种不同状态

select distinct status  from t_table
(NULL)
XXX
YYY

不同状态的记录数

select count(1) from t_table  35709
select count(1) from t_table where status  = 'XXX'      17579 XXX
select count(1) from t_table where status  = 'YYY'      4242  YYY
select count(1) from t_table where status  is null      13888 (NULL)
select count(1) from t_table where status  = null       0
select count(1) from t_table where status  is not null  21821 XXX+ YYY 
select count(1) from t_table where status  <> null      0
select count(1) from t_table where status  != null      0 

1

select count(1)  from t_table where status   != 'XXX'     4242   不含null值记录
select count(1)  from t_table where status   != 'YYY'     17579  不含null值记录
select count(1)  from t_table where status   != 'XXX' and  status   != 'YYY'   0    !=  不包含 null值的记录

2

select count(1) from t_table where status  in (select status  from t_table)                                   21821
select count(1) from t_table where status  in (select status  from t_table where status  = 'XXX' )             17579
select count(1) from t_table where status  in (select status  from t_table where status  = 'YYY' )             4242
select count(1) from t_table where status  in (select status  from t_table where status  in ('XXX','YYY') )    21821

3

select count(1) from t_table where status   in ('XXX' )  17579
select status  from t_table where status  not in ('XXX' )     4242      not in  是不包含 (默认null值)的记录  
select count(1) from t_table where status  not in ('')       21821        
select count(1) from t_table where status  not in (null)      0   not in (null)   返回空   ANSISQL(SQL-92)规定
select count(1) from t_table where status  in (null)          0   in (NULL)       返回空   ANSISQL(SQL-92)规定
select count(1) from t_table where status  is null          13888            查询 带有null值得记录   最好不要使用 in 
select count(1) from t_table where status  is not null      21821            使用了 in  是 不查询 null 的记录的

4

select status  from t_table where status  not in ('XXX','YYY')  (默认null值)       返回空结果集
select region,isNull(region,'11') from t_table where status  not in ('XXX','YYY')  (默认null值)  加了where 后面的条件查询不到记录  ,返回空结果集,region都没有,也不会进行null判断
select COALESCE(region,'11') from t_table where status  not in ('XXX','YYY')  (默认null值)    返回空结果集

5

select count(1) from t_table where status  not in (select status  from t_table )                     0
select isNull(region,'22') from t_table where status  not in ('XXX','YYY')                          显示(Null),其实是 返回的 空结果集
select count(1) from t_table where status  not in ('XXX','YYY')                                     0
select count(1) from t_table where status  not in (select status  from t_table where status  not in ('XXX','YYY') )   35709  not in (空结果集)  所以,能查出所有数据
select count(1) from t_table where status  in ( null)                                                               0   in (NULL)    ANSISQL(SQL-92)规定的Null值的比较取值结果都为False,既Null=Null取值也是False    
select count(1) from t_table where status  not in ( null)                                                           0   not in (NULL)  ANSISQL(SQL-92)规定的Null值的比较取值结果都为False,既Null=Null取值也是False
select count(1) from t_table where status  in (select status  from t_table where status  not in ('XXX','YYY') )       0      in  (默认null值)
select count(1) from t_table where status  <> (select status  from t_table where status  not in ('XXX','YYY') )       0      <>  (默认null值)
select count(1) from t_table where status  != (select status  from t_table where status  not in ('XXX','YYY') )       0      !=  (默认null值)
select count(1) from t_table where status  != null                        0    ANSISQL(SQL-92)规定
select count(1) from t_table where status  <> null                        0    ANSISQL(SQL-92)规定

默认情况下,推荐使用 IS NOT NULL去判断,因为SQL默认情况下对!= Null的判断会永远返回0行,但没有语法错误。

如果你一定想要使用!= Null来判断,需要加上这个语句:

set ANSI_NULLS off

所以我们要牢记:默认情况下做比较条件时使用关键字“is null”和“is not null”。

如果你一定要使用!= null来进行条件判断,需要加上这个命令语句:SET ANSI_NULLS OFF,这时数据库进入ANSI SQL非标准模式,你会发现IS NOT NULL 和 != null 是等效的了。

扫描二维码关注公众号,回复: 1532063 查看本文章

这里使用的是模式切换命令SET ANSI_NULLS[ON/OFF]。ON值采用ANSI SQL严格标准,OFF值采用非标准兼容模式。另外SET ANSI_DEFAULTS [ON/OFF]命令也可以实现标准的切换,只是这个命令控制的是一组符合SQL-92标准的设置,其中就包括Null值的标准。

默认情况下,数据库管理程序(DB-Library)是SET ANSI_NULLS为OFF的。但是我们的大多数应用程序,都是通过ODBC或者OLEDB来访问数据库的,作为一种开放兼容的数据库访问程序,或许是兼容性的考虑,SETANSI_NULLS值设置为ON。这样一来带来的一些问题是需要注意的。像存储过程或者自定义函数这样的应用程序都是基于DB-Library的,默认情况下,SETANSI_NULLS为OFF,并且在这样的程序中,不能使用SETANSI_NULLS在一个环境中修改规则,只能修改数据库配置参数。

执行 set ANSI_NULLS off

select count(1) from t_table where status  in ( null)                13888
select count(1) from t_table where status  not in ( null)            21821                                               
select count(1) from t_table where status  in (select status  from t_table where status  not in ('XXX','YYY') )   13888  
select count(1) from t_table where status  <> null                 21821
select count(1) from t_table where status  <> (select status  from t_table where status  not in ('XXX','YYY') )    
select status  from t_table where status  not in ('XXX','YYY')    返回  13888null值记录  导致下面的报错 

[Err] 21000 - [SQL Server]Subquery returned more than 1 value. 
This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.


set ANSI_NULLS on   时 ,下面sql  返回的是 null   ,不会出现语法报错的情况
select status  from t_table where status  not in ('XXX','YYY')

select count(1) from t_table where status  != (select status  from t_table where status  not in ('XXX','YYY') )  语法报错     

slq_不使用or查询 0 和null_ISNULL(a.status ,0)=0 和 case when else end

SQL中IS NOT NULL与!=NULL的区别

猜你喜欢

转载自blog.csdn.net/icecoola_/article/details/80623550