【问题标题】:What IsNull() means in the where clause?在 where 子句中 IsNull() 是什么意思?
【发布时间】:2022-01-28 00:27:17
【问题描述】:
product_id product_name product_description
1 Fans Panasonic fans
2 Refrigerator Toshiba refrigerator
3 Light Edison bulb
4 Laptop NULL

产品表如上图。

select * 
from product 
where isnull(product_description, null) = product_description

SQL 脚本是我要运行的。

我可以知道如何阅读或它对条件的实际含义吗?为什么没有选择第 4 行?感觉和查询一样

select * 
from product 
where product_description is not null

我对查询中的条件有点困惑。

【问题讨论】:

  • ISNULL(product_description, NULL) = product_description 没有意义,如果 product_description 是 NULL 它会毫无意义地用 NULL 替换它,然后它会执行 = 条件,对于 not null product_description 只会是 true .因此,正如您所怀疑的那样,这只是编写where product_description is not null 的一种不必要的混淆方式
  • 根据问题指南,请不要发布代码、数据、错误消息等的图像 - 将文本复制或输入到问题中。请保留将图像用于图表或演示渲染错误,无法通过文本准确描述的事情。
  • 在尝试理解 T-SQL 函数时,阅读docs 应该始终是您的第一站。

标签: sql sql-server tsql


【解决方案1】:

ISNULL 接受两个参数。它返回第一个参数,除非它为空,在这种情况下它返回第二个参数 - 所以写像 ISNULL(SomethingHere, NULL) 这样的东西是没有意义的 - 这意味着 where 子句与 where product_description = product_description 相同。

由于在 T-SQL 中NULL = NULL 返回UNKNOWN(相当于WHERE 子句上下文中的false),因此它与where product_description is not null 基本相同。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-16
    • 2012-09-09
    • 2011-08-29
    • 1970-01-01
    • 2011-06-05
    • 2011-05-12
    相关资源
    最近更新 更多