Mysql 简明教程

MySQL - IS NOT NULL Operator

MySQL 表中的 NULL 值表示缺失或未知值。它似乎是空白的,不包含任何数据。这不同于零值。

A NULL value in a MySQL table indicates a missing or unknown value. It appears to be blank and does not contain any data. This is different from zero values.

非常重要的是要明白一个 NULL 值不同于一个零值或一个包含空格的字段。为了检查表列中的 NULL 值,我们可以使用两个基本运算符:

It is very important to understand that a NULL value is different than a zero value or a field that contains spaces. To check for NULL values in a table column, we can use two basic operators:

  1. IS NULL

  2. IS NOT NULL

MySQL IS NOT NULL Operator

MySQL IS NOT NULL 运算符用于验证一个特定的列是否有一个非空值,或者是没有。这个运算符可以与诸如 SELECT、UPDATE 和 DELETE 这类 SQL 语句一起使用。

The MySQL IS NOT NULL operator is used to verify whether a particular column has a non-null value or not. This operator can be used with SQL statements such as SELECT, UPDATE, and DELETE.

通过在条件子句中使用 IS NOT NULL 运算符,我们仅能够获取包含特定列中有效数据的记录。

By using the IS NOT NULL operator in a conditional clause, we can only fetch the records that contain valid data in a particular column.

Syntax

以下是 MySQL 中 IS NOT NULL 的语法——

Following is the syntax of IS NOT NULL in MySQL −

SELECT column_name1, column_name2, ...
FROM table_name
WHERE column_name IS NOT 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 −

Example

在下面的查询中,我们要返回 CUSTOMERS 表中所有 AGE 不为空的记录。

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

SELECT * FROM CUSTOMERS
WHERE AGE IS NOT NULL;

Output

生成了以下输出——

Following output is produced −

IS NOT NULL with COUNT() function

我们可以将 IS NOT NULL 运算符与 MySQL COUNT() 函数一起使用,来只计数特定列中的非空值。

We can use the IS NOT NULL operator along with the MySQL COUNT() function to count only the non-null values in a specific column(s).

Syntax

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

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

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

Example

以下查询返回 CUSTOMERS 表中 ADDRESS 列不为空的所有行的计数。

The following query returns the count of all rows in the CUSTOMERS table where the ADDRESS column is not NULL.

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

Output

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

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

IS NOT NULL with UPDATE statement

在 MySQL 中,我们可以使用带有 IS NOT NULL 运算符的 UPDATE 语句,来更新特定列中的所有非空行。

In MySQL, we can update all the non-null rows in a specific column(s) using the UPDATE statement with IS NOT NULL operator.

Syntax

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

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

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

Example

在下面的查询中,我们将 SALARY 列更新为 9000 值,用于 SALARY 列不为空的所有记录——

In the following query, we will update the SALARY column to a value of 9000 for all records where the SALARY column is not NULL −

UPDATE CUSTOMERS
SET SALARY = 20000
WHERE SALARY IS NOT NULL;

Verification

为了检查这个表是否已经被更新,执行 SELECT 查询来显示 CUSTOMERS 表。

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

IS NOT NULL with DELETE statement

在 MySQL 中,我们可以使用带有 IS NOT NULL 运算符的 DELETE 语句,来删除特定列中的所有非空行。

In MySQL, we can delete all the non-null rows in a specific column(s) using the DELETE statement with IS NOT NULL operator.

Syntax

以下是 MySQL 中带 DELETE 语句的 IS NOT NULL 运算符的语法 -

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

DELETE FROM table_name
WHERE columnname1, columnname2, ... IS NOT NULL;

Example

在以下查询中,我们尝试删除 CUSTOMERS 表的 AGE 列中不为 NULL 的记录。

In the following query, we are trying to delete records which are not null in the AGE column of CUSTOMERS table.

DELETE FROM CUSTOMERS
WHERE AGE IS NOT NULL;

Verification

为验证表是否已更新,请使用 SELECT 查询显示表。

To verify whether the table has been updated or not, display the table using a SELECT query.

IS NOT NULL Operator Using Client Program

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

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

Syntax

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

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

Example

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

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