zoukankan      html  css  js  c++  java
  • not in查询不出数据问题

       

    select ID from SmartCustomer where ID not in (select distinct CustomerID from SmartPromoter where CustomerID)
    改为
    select ID from SmartCustomer where ID not in (select distinct CustomerID from SmartPromoter where CustomerID is not null)

    这个问题的根源在于null,众所周知,当判断一个值是否为null的时候,sql server要用is null 或者is not null,  在SQL Server中,Null值并不是一个值,而是表示特定含义,其所表示的含义是“Unknow”,可以理解为未定义或者未知,因此任何与Null值进行比对的二元操作符结果一定为Null,包括Null值本身。而在SQL Server中,Null值的含义转换为Bool类型的结果为False。 SQL Server提供了“IS”操作符与Null值做对比,用于衡量某个值是否为Null。

    这里还要说一下not in的问题,应尽量避免使用not in,not in结果不准确,性能效率低。可以采用not exists方案替代

    select ID from SmartCustomer a where  not exists (select ID from SmartPromoter b where a.ID=b.CustomerID)
  • 相关阅读:
    sqlServer的主键只能自增不能手动增加
    TP函数
    TP复习17
    TP复习16
    TP复习15
    TP复习14
    TP复习13
    TP复习12
    TP复习11
    TP复习10
  • 原文地址:https://www.cnblogs.com/xiaopotian/p/6826628.html
Copyright © 2011-2022 走看看