SQL 使用 Null 值更新列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3923721/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Update columns with Null values
提问by nath
I tried updating a table as follows:
我尝试按如下方式更新表格:
update userloginstats set logouttime = sysdate where logouttime = null;
It didn't update the columns with null values. What went wrong?
它没有用空值更新列。什么地方出了错?
回答by codaddict
Change it to
将其更改为
...where logouttime is null;
^^^^^^^
NULL
is a special value and we cannot use the usual =
operator with it.
NULL
是一个特殊值,我们不能=
对它使用通常的运算符。
From the Oracle documentation for NULL:
To test for nulls, use onlythe comparison conditions IS NULL and IS NOT NULL. If you use any other condition with nulls and the result depends on the value of the null, then the result is UNKNOWN because null represents a lack of data, a null cannotbe equal or unequal to any value or to another null
要测试空值,请仅使用比较条件 IS NULL 和 IS NOT NULL。如果您对空值使用任何其他条件并且结果取决于空值的值,则结果为 UNKNOWN,因为空值表示缺少数据,空值不能等于或不等于任何值或另一个空值
回答by Lucero
You cannot compare NULLs with =.
您不能将 NULL 与 = 进行比较。
Use this:
用这个:
update userloginstats set logouttime= sysdate where logouttime is null;
回答by Donnie
logouttime is null
, not = null
. null
is never equal to anything, not even itself. Thus, the operator is
.
logouttime is null
,不是= null
。 null
永远不等于任何东西,甚至不等于它本身。因此,运营商is
.
回答by Macros
You need to use is null
not = null
你需要使用is null
不= null
update userloginstats set logouttime= sysdate where logouttime is null;
回答by Maltronic
For nulls you must use "IS NULL" or "IS NOT NULL" rather than the = operator. This is because null is technically neither true or false, rather it's the absence of a value either way.
对于空值,您必须使用“ IS NULL”或“ IS NOT NULL”而不是 = 运算符。这是因为 null 在技术上既不是 true 也不是 false,而是两种方式都没有值。
Most programming languages typically associate null with false for convenience (and thereby enabling the use of the = operator), but SQL takes a more purist approach, rightly or wrongly.
大多数编程语言通常为方便起见将 null 与 false 相关联(从而允许使用 = 运算符),但 SQL 采取了更纯粹的方法,无论正确与否。