Mysql 简明教程

MySQL - IS NULL Operator

MySQL 表中的 NULL 值表示其中没有(或未知)值。这些值与零或无效值不同。

NULL values in a MySQL table fields indicate that no (or unknown) values are present in them. These values are different from zeroes or invalid values.

在 MySQL 中,无法使用相等运算符(例如 =、< 或 <>)检查 NULL 值。相反,我们使用 IS NULL 和 IS NOT NULL(过滤非空值)运算符。

In MySQL, it is not possible to check NULL values with comparison operators such as =, <, or <>. Instead, we use the IS NULL and IS NOT NULL (filtering non-null values) operators.

MySQL IS NULL Operator

MySQL 中的 IS NULL 运算符用于检查列中的值是否为 NULL。将 IS NULL 运算符与条件子句结合使用,可以过滤包含特定列中 NULL 值的记录。

The IS NULL operator in MySQL is used to check whether a value in a column is NULL. Using the IS NULL operator with a conditional clause allows us to filter records that contain NULL values in a particular column.

我们还可以在 SELECT、UPDATE 和 DELETE SQL 语句中使用此运算符。

We can also use this operator with SELECT, UPDATE, and DELETE SQL statements.

Syntax

以下是 MySQL 中 IS NULL 的语法 −

Following is the syntax of IS NULL in MySQL −

SELECT column_name1, column_name2, ...
FROM table_name
WHERE column_name IS NULL;

Example

首先,让我们使用以下查询创建一个名为 CUSTOMERS 的表 −

Firstly, let us create a table named CUSTOMERS using the following query −

CREATE TABLE CUSTOMERS (
   ID INT NOT NULL,
   NAME VARCHAR (20) NOT NULL,
   AGE INT,
   ADDRESS CHAR (25),
   SALARY DECIMAL (18, 2),
   PRIMARY KEY (ID)
);

在以下查询中,我们使用 INSERT 语句向表中插入值 −

In the following query, we are using the INSERT statement to insert values to the table −

INSERT INTO CUSTOMERS VALUES
(1, 'Ramesh', 32, 'Ahmedabad', NULL),
(2, 'Khilan', 25, 'Delhi', 1500.00),
(3, 'Kaushik', NULL, 'Kota', 2000.00),
(4, 'Chaitali', 25, 'Mumbai', NULL),
(5, 'Hardik', 27, 'Bhopal', 8500.00),
(6, 'Komal', NULL, 'Hyderabad', 4500.00),
(7, 'Muffy', 24, 'Indore', 10000.00);

该表创建如下 −

The table is created as follows −

IS NULL with SELECT statement

MySQL IS NULL 运算符可以与 SELECT 语句结合使用,以筛选具有 NULL 值的记录。

The MySQL IS NULL operator can be used with the SELECT statement to filter the records with NULL values.

Example

在以下查询中,我们将从 CUSTOMERS 表中返回 AGE 为 null 的所有记录。

In the following query, we are going to return all the records from the CUSTOMERS table where the AGE is null.

SELECT * FROM CUSTOMERS
WHERE AGE IS NULL;

Output

执行上述查询后,它将生成如下所示的输出 −

On executing the above query, it will generate an output as shown below −

IS NULL with COUNT() function

我们可以在 MySQL 中使用 IS NULL 运算符和 COUNT() 函数计算特定列中具有 NULL 值的记录数。

We can use the MySQL IS NULL operator with the COUNT() function to count the number of records with NULL values in a particular column.

Syntax

以下是 MySQL 中 IS NULL 与 COUNT() 函数的语法 −

Following is the syntax of the IS NULL with COUNT() function in MySQL −

SELECT COUNT(column_name1, column_name2, ...)
FROM table_name
WHERE condition IS NULL;

Example

以下查询返回 CUSTOMERS 表的 ADDRESS 列中空白字段(NULL)的记录数。

The following query returns the count of records have a blank field (NULL) in ADDRESS column of the CUSTOMERS table.

SELECT COUNT(*) FROM CUSTOMERS
WHERE ADDRESS IS NULL;

Output

执行上述查询后,它将生成如下所示的输出 −

On executing the above query, it will generate an output as shown below −

IS NULL with UPDATE statement

在 MySQL 中,我们可以将 IS NULL 运算符与 UPDATE 语句结合使用,以更新特定列中具有 NULL 值的记录。

In MySQL, we can use the IS NULL operator with the UPDATE statement to update records with NULL values in a particular column.

Syntax

以下是 MySQL 中 IS NULL 运算符与 UPDATE 语句的语法 −

Following is the syntax of the IS NULL operator with the UPDATE statement in MySQL -

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE columnname1, columnname2, ... IS NULL;

Example

在以下查询中,我们正在将 SALARY 列的空白(NULL)记录更新为 9000 的值。

In the following query, we are updating the blank (NULL) records of the SALARY column to a value of 9000.

UPDATE CUSTOMERS
SET SALARY = 9000
WHERE SALARY IS NULL;

Verification

要检查表是否已更新,请执行 SELECT 查询以显示表。

To check whether the table has been updated or not, execute the SELECT query to display the table.

IS NULL with DELETE statement

在 MySQL 中,我们可以将 IS NULL 运算符与 DELETE 语句结合使用,删除特定列中具有 NULL 值的记录。

In MySQL, we can use the IS NULL operator with the DELETE statement to delete records with NULL values in a particular column.

Syntax

以下是在 MySQL 中使用 IS NULL 运算符和 DELETE 语句的语法 -

Following is the syntax of the IS NULL operator with the DELETE statement in MySQL -

DELETE FROM table_name
WHERE column_name(s) IS NULL;

Example

在以下查询中,我们试图删除 CUSTOMERS 表的 ADDRESS 列中存在的空 (NULL) 记录。

In the following query, we are trying to delete the blank (NULL) records present in the ADDRESS column of CUSTOMERS table.

DELETE FROM CUSTOMERS
WHERE AGE IS NULL;

Verification

若要查看表是否已更改,请执行 SELECT 查询以显示该表。

To check whether the table has been changed or not, execute the SELECT query to display the table.

IS NULL Operator Using Client Program

除了使用 SQL 查询在 MySQL Server 上执行 IS NULL 运算符外,我们还可以使用客户端程序执行它。

In addition to executing the IS NULL Operator on a MySQL Server using SQL query, we can also execute it using a client program.

Syntax

以下是 MySQL 表中在各种编程语言中的 IS NULL 运算符语法 -

Following are the syntaxes of the IS NULL Operator in MySQL table in various programming languages −

Example

以下是该操作在各种编程语言中的实现 −

Following are the implementations of this operation in various programming languages −