Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL:;“条件不正确”;“替换”的模式;是否为空(条件)“;_Sql_Postgresql_Null_Comparison - Fatal编程技术网

SQL:;“条件不正确”;“替换”的模式;是否为空(条件)“;

SQL:;“条件不正确”;“替换”的模式;是否为空(条件)“;,sql,postgresql,null,comparison,Sql,Postgresql,Null,Comparison,在SQL中(特别是Postgres): 如果foo为null,则子句where not foo='bar'将计算为某种null,导致结果中不包括该行 另一方面,子句where(foo='bar')不为truewherefoo为null,其计算结果为true(该行包含在结果中) 那么,(foo='bar')的用法是否不正确等同于foo为null或不是foo='bar' 人们使用这种模式似乎不是出于某种原因,我是不是遗漏了什么 那么,(foo='bar')的用法是否不正确等同于foo为null或不是

在SQL中(特别是Postgres):

如果
foo
为null,则子句
where not foo='bar'
将计算为某种
null
,导致结果中不包括该行

另一方面,子句
where(foo='bar')不为true
where
foo
为null,其计算结果为
true
(该行包含在结果中)

那么,
(foo='bar')的用法是否不正确
等同于
foo为null或不是foo='bar'

人们使用这种模式似乎不是出于某种原因,我是不是遗漏了什么

那么,
(foo='bar')的用法是否不正确
等同于
foo为null或不是foo='bar'

对。或更简单:

foo IS NULL OR foo <> 'bar'
使用后者。
手册章节中的详细信息
相关的:

    • 这个怎么样

      where coalesce(foo, 'foo') <> 'bar'
      
      我怀疑人们正在避免
      不正确
      ,因为它在Oracle和MS SQL Server中不起作用。使用MS SQL Server的用户将避免使用
      ISNULL
      函数的原因也是如此

      评论回复:

      我不知道你说的不可能的“foo”是什么意思。你是说一个不可能的值吗?不是真的。它只需要不是“酒吧”。 这仍然有效:

      declare @foo varchar(15)
      declare @bar varchar(15)
      set @bar = 'bar'
      --set @foo = 'bar'
      --set @foo = 'asdf'
      
      select 7
      
      where coalesce( @foo, 'not' + @bar) <> @bar
      
      declare@foo varchar(15)
      声明@bar varchar(15)
      设置@bar='bar'
      --设置@foo='bar'
      --设置@foo='asdf'
      选择7
      在哪里合并(@foo,'不是'+@bar)@bar
      
      作为旁注,
      不同于
      不能使用索引。但是,
      foo为NULL或foo'bar'
      可能会使用索引(如果合适)作为旁注的旁注:像
      foo这样的条件不同于'bar'
      即使在技术上可行,也很少会使用索引,因为它通常适用于大多数行,而这正是索引通常没有帮助的地方。True,但是,当“bar”被一个参数(典型用例)替换时,您需要一个“不可能”的“foo”。
      where not coalesce(foo, 'foo') = 'bar'
      
      declare @foo varchar(15)
      declare @bar varchar(15)
      set @bar = 'bar'
      --set @foo = 'bar'
      --set @foo = 'asdf'
      
      select 7
      
      where coalesce( @foo, 'not' + @bar) <> @bar