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.
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;
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;
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;
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.