使用不为空时的结果不同

时间:2017-01-23 03:52:03

标签: sql-server

当我使用此查询时:

SELECT * from  [dbo].[CRA] 
where [gender] not like 'null'
      and [house] not like 'null'
      and [residenttime] is not null
      and [worktime] is not null
      and [loaiDN] not like 'null'
      and [depend] not like 'null'
      and [expGD] not like 'null'
      and [Grincome] not like 'null'
      and [dunocacTCTD] not like 'null'
      and [tinhtrangno] not like 'null'
      and [tgQHTD] not like 'null'
      and [soduTB] not like 'null'
      and [TlquaMB] not like 'null'

结果:81行

但使用此查询时:

example:
  adapter: postgresql
  encoding: unicode
  database: example_dev

example_report:
  adapter: postgresql
  encoding: unicode
  database: example_report_dev

删除的行数是> 1000行

为什么两个结果不同?

3 个答案:

答案 0 :(得分:2)

!(A and B) = !A or !B以来SELECT * from [dbo].[CRA] where [gender] not like 'null' or [house] not like 'null' or [residenttime] is not null or [worktime] is not null or [loaiDN] not like 'null' or [depend] not like 'null' or [expGD] not like 'null' or [Grincome] not like 'null' or [dunocacTCTD] not like 'null' or [tinhtrangno] not like 'null' or [tgQHTD] not like 'null' or [soduTB] not like 'null' or [TlquaMB] not like 'null'

对面将是

SELECT * from  [dbo].[CRA] 
where not ([gender] like 'null'
      and [house] like 'null'
      and [residenttime] is  null
      and [worktime] is null
      and [loaiDN] like 'null'
      and [depend] like 'null'
      and [expGD] like 'null'
      and [Grincome] like 'null'
      and [dunocacTCTD] like 'null'
      and [tinhtrangno] like 'null'
      and [tgQHTD] like 'null'
      and [soduTB] like 'null'
      and [TlquaMB] like 'null');

或简单地说,

$scope.cateClicked

答案 1 :(得分:1)

我相信你正在与你的行等字符串进行比较,比如

and [house] not like 'null'

取而代之的是关闭引号,使用null或不为null

where Field is null
where Field is not null

答案 2 :(得分:0)

这是另一个样本:

    SELECT * from  [dbo].[CRA] 
    where COALESCE( NULLIF([gender],'null')
                   ,NULLIF([house],'null')
                   ,[residenttime]
                   ,[worktime]
                   ,NULLIF([loaiDN],'null')
                   ,NULLIF([depend],'null')
                   ,NULLIF([expGD],'null')
                   ,NULLIF([Grincome],'null')
                   ,NULLIF([dunocacTCTD],'null')
                   ,NULLIF([tinhtrangno],'null')
                   ,NULLIF([tgQHTD],'null')
                   ,NULLIF([soduTB],'null')
                   ,NULLIF([TlquaMB],'null')
                   ) IS NULL --Will be returned only all the values is null or 'null'

<强> BUT

           where COALESCE( NULLIF([gender],'null')
           ,NULLIF([house],'null')
           ,[residenttime]
           ,[worktime]
           ,NULLIF([loaiDN],'null')
           ,NULLIF([depend],'null')
           ,NULLIF([expGD],'null')
           ,NULLIF([Grincome],'null')
           ,NULLIF([dunocacTCTD],'null')
           ,NULLIF([tinhtrangno],'null')
           ,NULLIF([tgQHTD],'null')
           ,NULLIF([soduTB],'null')
           ,NULLIF([TlquaMB],'null')
           ) IS NOT NULL --It will be returned at least on value is not null('null')