-2

我已经意识到如果 'fk_building' 为空,这两个查询不起作用。我不明白为什么,而且似乎找不到一个好的解释,因为谷歌有点难。有人可以解释为什么地狱!= 3 不返回所有不是 3 的东西吗???包括空行?为什么我需要使用 <=> ?

update floor set fk_building = 3 where fk_building != 3 and floor_id = 1;

或者

select * from floor where fk_building != 3

在 fk_building 为空的情况下不起作用。

4

4 回答 4

5

我建议查看WikipediaNULL上有关价值观的“常见错误”部分。

从条目:

For example, a WHERE clause or conditional statement might compare a column's value with a constant. It is often incorrectly assumed that a missing value would be "less than" or "not equal to" a constant if that field contains Null, but, in fact, such expressions return Unknown

As users have suggested, you can use a null safe operator if your RDBMS allows it, or check for IS NULL.

于 2012-09-07T18:13:16.533 回答
1

使用 null 安全运算符

 update floor set fk_building = 3 where (not fk_building <=> 3) and floor_id = 1;

http://dev.mysql.com/doc/refman/5.5/en/comparison-operators.html

于 2012-09-07T18:08:45.460 回答
0

与其生气,不如干脆做:

update floor set fk_building = 3 where (fk_building != 3 OR fk_building IS NULL) and floor_id = 1;
于 2012-09-07T18:07:16.850 回答
0

您也可以使用 x <=> 3 来测试两者。

编辑:在你的情况下,它是 'not x <=> 3'

于 2012-09-07T18:09:59.157 回答