set Null to value when use Is null in where

set Null to value when use Is null in where

我明白了 table :

我在什么地方使用它:

where FoodID=1

当我用这个改变我的位置时:

where FoodID=1 and DayId=1 or DayId is null

所有行的已归档 dayid 值更改为 null。

注:

My where Statement generate with Special Framework : I write this Code in framework :

@@wheresql or dayid is null

我不知道为什么在 use 为 null 时将值更改为 null?

您必须在 or 运算符周围使用 ()

where FoodID=1 and (DayId=1 or DayId is null)

如果不是首先评估 and 运算符,然后 or

 where FoodID=1 and (DayId=1 or DayId is null)

你必须小心处理栅栏和逻辑运算符,因为它们有优先级(AND 比 OR 运算符有更高的优先级),所以它们是不一样的:

where FoodID=1 and (DayId=1 or DayId is null)

where (FoodID=1 and DayId=1) or DayId is null

如果你在你的表达式中省略了栅栏,那么你的表达式就会变成第二种形式,所以你必须明确定义表达式验证的逻辑顺序,它是:

where FoodID=1 and (DayId=1 or DayId is null)

像这样使用ISNULL

where FoodID=1 and ISNULL(DayId,1) = 1

这可能会删除您在 where 条款中的条件之一。

这里的问题是运算符优先级。布尔值 and 优先于 or,因此 FoodID=1 and DayId=1 or DayId is null 被解释为 (FoodID=1 and DayId=1) or DayId is null。由于您的 table 没有任何包含 FoodID=1 and DayId=1 的行,因此仅返回包含 DayId is null 的行。

如果要查询带有 FoodID=1DayId1null 的行,则需要显式使用方括号:FoodID=1 and (DayId=1 or DayId is null)