【发布时间】:2014-10-31 14:31:23
【问题描述】:
我使用的是 SQL Server 2008,当我对外部表没有任何记录的表执行左连接时,我在 where 子句中看到了奇怪的行为。如果我检查外部表中的值是否为“非空”,它有时会返回 true。
select *
from foo f left join bar b on b.id=f.id
where f.id=@id and (f.status = 1 or b.Price is not null)
当我的 f.status = 0 和 b.Price 不存在(或者,在选择中显示为 null)时,此查询将选择 f.status = 0 和 b.Price is null 的记录,即使 (FALSE OR FALSE) 应该为 FALSE。
如果我只是执行此查询,它会按预期工作,并且不会选择“bar”中没有记录的任何内容。
select *
from foo f left join bar b on b.id=f.id
where f.id=@id and b.Price is not null
将b.Price is not null 作为or 操作的一部分似乎由于某种原因导致了问题。这个查询可能有什么问题?我在 SQL Server 2012 机器上使用类似数据运行相同的查询,但没有看到这个问题,这可能与我使用的 SQL Server 版本有关吗?
【问题讨论】:
标签: sql sql-server sql-server-2008