The 在您习惯它之前,NULL
value can be surprising until you get used to it. NULL
值可能会令人惊讶。Conceptually, 从概念上讲,NULL
means “a missing unknown value” and it is treated somewhat differently from other values.NULL
表示“缺少未知值”,它的处理方式与其他值有所不同。
To test for 要测试NULL
, use the IS NULL
and IS NOT NULL
operators, as shown here:NULL
,请使用IS NULL
和IS NOT NULL
运算符,如下所示:
mysql> SELECT 1 IS NULL, 1 IS NOT NULL;
+-----------+---------------+
| 1 IS NULL | 1 IS NOT NULL |
+-----------+---------------+
| 0 | 1 |
+-----------+---------------+
You cannot use arithmetic comparison operators such as 不能使用算术比较运算符,如=
, <
, or <>
to test for NULL
. =
、<
或<>
测试是否为NULL
。To demonstrate this for yourself, try the following query:要亲自演示这一点,请尝试以下查询:
mysql> SELECT 1 = NULL, 1 <> NULL, 1 < NULL, 1 > NULL;
+----------+-----------+----------+----------+
| 1 = NULL | 1 <> NULL | 1 < NULL | 1 > NULL |
+----------+-----------+----------+----------+
| NULL | NULL | NULL | NULL |
+----------+-----------+----------+----------+
Because the result of any arithmetic comparison with 由于使用NULL
is also NULL
, you cannot obtain any meaningful results from such comparisons.NULL
进行任何算术比较的结果也是NULL
,因此您无法从此类比较中获得任何有意义的结果。
In MySQL, 在MySQL中,0
or NULL
means false and anything else means true. 0
或NULL
表示false
,其他任何内容都表示true
。The default truth value from a boolean operation is 布尔运算的默认真值为1
.1
。
This special treatment of 这种对NULL
is why, in the previous section, it was necessary to determine which animals are no longer alive using death IS NOT NULL
instead of death <> NULL
.NULL
的特殊处理就是在上一节中有必要使用death IS NOT NULL
而不是death <> NULL
来确定哪些动物不再活着的原因。
Two 在NULL
values are regarded as equal in a GROUP BY
.GROUP BY
中,两个NULL
值被视为相等。
When doing an 在执行ORDER BY
, NULL
values are presented first if you do ORDER BY ... ASC
and last if you do ORDER BY ... DESC
.ORDER BY
时,如果执行ORDER BY... ASC
,则NULL
值最先出现,如果执行ORDER BY ... DESC
,则NULL
值最后出现。
A common error when working with 使用NULL
is to assume that it is not possible to insert a zero or an empty string into a column defined as NOT NULL
, but this is not the case. NULL
时的一个常见错误是,假设不可能将零或空字符串插入定义为NOT NULL
的列中,但事实并非如此。These are in fact values, whereas 这些实际上是值,而NULL
means “not having a value.” NULL
表示“没有值”。You can test this easily enough by using 使用IS [NOT] NULL
as shown:IS [NOT] NULL
可以很容易地测试这一点,如图所示:
mysql> SELECT 0 IS NULL, 0 IS NOT NULL, '' IS NULL, '' IS NOT NULL;
+-----------+---------------+------------+----------------+
| 0 IS NULL | 0 IS NOT NULL | '' IS NULL | '' IS NOT NULL |
+-----------+---------------+------------+----------------+
| 0 | 1 | 0 | 1 |
+-----------+---------------+------------+----------------+
Thus it is entirely possible to insert a zero or empty string into a 因此,完全可以在NOT NULL
column, as these are in fact NOT NULL
. NOT NULL
列中插入零或空字符串,因为它们实际上是NOT NULL
。See Section B.3.4.3, “Problems with NULL Values”.请参阅第B.3.4.3节,“NULL值问题”。