[英]MySQL SELECT column FROM table WHERE column IS NULL
This is not working for me, using Toad
for MySQL
. 使用Toad
for MySQL
,这对我不起作用。 I'm using MySQL 5.5
from XAMPP 1.83
on Windows. 我正在Windows上使用XAMPP 1.83
MySQL 5.5
。
I have a table with column InstitutionState
defined as VARCHAR(20)
. 我有一个表,列InstitutionState
定义为VARCHAR(20)
。 Some rows appear to have this column "empty", meaning LENGTH(InstitutionState) = 0
. 某些行似乎具有此列“空”,这意味着LENGTH(InstitutionState) = 0
。
If I SELECT ... WHERE InstitutionState IS NULL
, I get no rows. 如果我SELECT ... WHERE InstitutionState IS NULL
,则不会获得任何行。
If I SELECT ... WHERE InstitutionState = ''
, It works. 如果我SELECT ... WHERE InstitutionState = ''
,那么它将起作用。 Why is this? 为什么是这样?
Here's sample data. 这是示例数据。
mysql> select InstitutionState, ISNULL(InstitutionState), length(InstitutionState)
-> from institution;
+----------------------+--------------------------+--------------------------+
| InstitutionState | ISNULL(InstitutionState) | length(InstitutionState) |
+----------------------+--------------------------+--------------------------+
| NY | 0 | 2 |
| NY | 0 | 2 |
| NY | 0 | 2 |
| IL | 0 | 2 |
| NC | 0 | 2 |
| TX | 0 | 2 |
| DC | 0 | 2 |
| NY | 0 | 2 |
| CA | 0 | 2 |
| | 0 | 0 |
| KS | 0 | 2 |
| | 0 | 0 |
| NY | 0 | 2 |
| ND | 0 | 2 |
| PA | 0 | 2 |
| WI | 0 | 2 |
| PA | 0 | 2 |
| MD | 0 | 2 |
| IN | 0 | 2 |
| PA | 0 | 2 |
| NE | 0 | 2 |
| ID | 0 | 2 |
| CA | 0 | 2 |
| | 0 | 0 |
| FL | 0 | 2 |
| MO | 0 | 2 |
| | 0 | 0 |
| OH | 0 | 2 |
| IL | 0 | 2 |
| OH | 0 | 2 |
Conceptually, NULL
means “a missing unknown value” 从概念上讲, NULL
表示“缺少未知值”
OR 要么
NULL
means no data, emptiness, nothing, unknown, missing value, etc . NULL
表示无数据,空虚,无,未知,缺失值等 。 The value empty string means an empty string. 空字符串表示空字符串。
NULL
value and the empty string may cause data integrity problem. 混淆NULL
值和空字符串可能会导致数据完整性问题。 What NULL
means in the context of a relational database is that the pointer to the character field is set to 0x00
in the row's header, therefore no data to access. 在关系数据库的上下文中, NULL
意思是,指向字符字段的指针在行的标题中设置为0x00
,因此没有数据可访问。
NULL
and ''
take up the exact same number of bytes on the disk. NULL
和''
占用磁盘上完全相同的字节数。 Hence, there is no space savings. 因此,没有节省空间。
NULL
values. 您可以在可以具有NULL
值的列上添加索引。 Otherwise, you must declare an indexed column NOT NULL
, and you cannot insert NULL
into the column. 否则,必须声明一个索引列NOT NULL
,并且不能在该列中插入NULL
。 Furthermore, allowing NULL
is a less restrictive configuration than disallowing NULL
. 此外,与不允许NULL
相比,允许NULL
的限制较少。 It only follows that if any entity integrity issues are to arise, it would be from FEWER checks that the data are sound. 只有这样,如果出现任何实体完整性问题,就可以通过FEWER检查数据是否正确。 Therefore, logically, allowing NULL should always have a good, solid reason, and disallowing NULL is a good practice. 因此,从逻辑上讲,允许NULL应该始终有一个充分的充分理由,而不允许NULL是一个好习惯。
mysql> INSERT INTO ... (InstitutionState) VALUES (NULL);
mysql> INSERT INTO ... (InstitutionState) VALUES ('');
Both statements will insert a value into the InstitutionState column, but the first inserts a NULL
value and the second inserts an empty string. 这两个语句都会在InstitutionState列中插入一个值,但是第一个语句将插入NULL
值,第二个语句将插入一个空字符串。 The meaning of the first can be regarded as “InstitutionState is not known” and the meaning of the second can be regarded as “the Institution is known to have no state, and thus no InstitutionState.” 第一个的含义可以视为“未知机构状态”,第二个的含义可以视为“已知机构不具有状态,因此也没有InstitutionState”。
To search for column values that are NULL
, you cannot use an expr = NULL
test. 要搜索为NULL
列值,不能使用expr = NULL
测试。 The following statement returns no rows, because expr = NULL is never true for any expression: 以下语句不返回任何行,因为expr = NULL对于任何表达式都不是真的:
mysql> SELECT ... WHERE InstitutionState = NULL;
To look for NULL
values, you must use the IS NULL
test. 要查找NULL
值,必须使用IS NULL
测试。 The following statements show how to find the NULL
InstitutionState and the empty InstitutionState: 以下语句显示如何查找NULL
InstitutionState和空InstitutionState:
mysql> SELECT ... WHERE InstitutionState IS NULL;
mysql> SELECT ... WHERE InstitutionState = '';
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 |
+----------+-----------+----------+----------+
In addition, 此外,
To get ''
AND NULL
s, 要获得''
AND NULL
,
We would use: 我们将使用:
SELECT ... WHERE IFNULL(InstitutionState , '') = '';
Which says if the field is NULL
pretend that it is an empty string ie ''
. 它说如果该字段为NULL
,则假装它是一个空字符串,即''
。
The NULL
value isn't an actual value in SQL, but the lack of a value. NULL
值不是SQL中的实际值,而是缺少值。 One can think of it as unknown . 人们可以认为这是未知的 。 For that reason, not even NULL
is equal to another null . 因此,甚至NULL
都不等于另一个null 。
Null values are actually implemented as a bitmask on the row, which indicate which columns have null values. 空值实际上是作为行上的位掩码实现的,它指示哪些列具有空值。 So, these values aren't even stored on the heap table in the same way as other values, which is one of the reasons why you have to explicitly declare a column as nullable . 因此,这些值甚至没有以与其他值相同的方式存储在堆表中,这就是为什么必须将一列显式声明为nullable的原因之一。
The string ''
is actually known. 字符串''
实际上是已知的。 It's known to be ''
. 据了解是''
。 This isn't null
, nor is that null bit set on the tuple. 这不是null
,也不在元组上设置了null位 。
For this reason, querying for rows where a column IS NULL
will not return rows with a value of ''
nor will querying for rows where a column is ''
return null values. 出于这个原因,查询行,其中一列IS NULL
不会的值返回行''
也不会为行查询,其中一栏是''
返回空值。 They are two completely different things. 它们是完全不同的两件事。
There are actually a few exceptions. 实际上有一些例外。 For example, in Oracle, any reference to ''
will be implicitly cast to NULL
. 例如,在Oracle中,对''
任何引用都将隐式转换为NULL
。 This behavior was implemented back in the 80s before a real SQL standard, so Oracle has had to maintain it for backwards compatibility reasons. 此行为早在80年代就已在真正的SQL标准之前实现,因此Oracle出于向后兼容的原因不得不对其进行维护。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.