zoukankan      html  css  js  c++  java
  • sql 在not in 子查询有null值情况下经常出现的陷阱

    如果下:TempSalesPriceFixedValues表和SalesPriceFixedValues表,要求查询出在TempSalesPriceFixedValues表中且不在SalesPriceFixedValues表中的记录。

    select distinct Ctyp from TempSalesPriceFixedValues where  Ctyp not in  (
    select distinct ConditonTypeCode from SalesPriceFixedValues
    )

    查询结果:

    我们看到没有数据,因为SalesPriceFixedValues表里有NULL值。

    select distinct ConditonTypeCode from SalesPriceFixedValues

    正确的写法:

    select distinct Ctyp from TempSalesPriceFixedValues where  Ctyp not in  (
    select distinct ConditonTypeCode from SalesPriceFixedValues where ConditonTypeCode is not null
    )

    原因:

    NULL不能进行某些逻辑操作:

    –如果null参与算术运算,则该算术表达式的值为null。(例如:+,-,*,/ 加减乘除)

    –如果null参与比较运算,则结果可视为false。(例如:>=,<=,<>  大于,小于,不等于)

    –如果null参与聚集运算,则聚集函数都置为null。除count(*)之外。

    --如果在not in子查询中有null值的时候,则不会返回数据。

    勤劳一日,便得一夜安眠;勤劳一生,便得幸福长眠。
  • 相关阅读:
    Elastic Search(一)
    嵌入式jetty
    mybatis中的#{}和${}的区别
    拦截器和过滤器的区别
    springboot对拦截器的支持
    Springboot对filter的使用
    springboot对监听器Listener的使用
    随机数的基本概念
    hashset和treeset区别
    java中常见的api方法记录
  • 原文地址:https://www.cnblogs.com/zhaomengmeng/p/4938864.html
Copyright © 2011-2022 走看看