MySQL 查询删除列中的空记录?

mysqlmysqli database更新于 2025/1/18 4:28:17

要删除列中的空记录,可以使用 delete 命令。以下是语法 −

delete from yourTableName where yourColumnName IS NULL;

首先我们创建一个表 −

mysql> create table removeNullRecordsDemo
   -> (
   -> Name varchar(100)
   -> );
Query OK, 0 rows affected (0.50 sec)

下面是使用 insert 命令向表中插入记录的查询 −

mysql> insert into removeNullRecordsDemo values('John');
Query OK, 1 row affected (0.14 sec)

mysql> insert into removeNullRecordsDemo values(null);
Query OK, 1 row affected (0.15 sec)

mysql> insert into removeNullRecordsDemo values('Larry');
Query OK, 1 row affected (0.19 sec)

mysql> insert into removeNullRecordsDemo values('Bob');
Query OK, 1 row affected (0.12 sec)

mysql> insert into removeNullRecordsDemo values(null);
Query OK, 1 row affected (0.13 sec)

mysql> insert into removeNullRecordsDemo values('David');
Query OK, 1 row affected (0.18 sec)

mysql> insert into removeNullRecordsDemo values(null);
Query OK, 1 row affected (0.22 sec)

以下是使用 select 语句显示表中的所有记录的查询 −

mysql> select *from removeNullRecordsDemo;

这将产生以下输出 −

+-------+
| Name  |
+-------+
| John  |
| NULL  |
| Larry |
| Bob   |
| NULL  |
| David |
| NULL  |
+-------+
7 rows in set (0.00 sec)

现在让我们删除上面列中的 NULL 记录 −

mysql> delete from removeNullRecordsDemo where Name IS NULL;
Query OK, 3 rows affected (0.16 sec)

检查是否已从列中删除了 NULL 记录 −

mysql> select * from removeNullRecordsDemo;

以下是显示除 NULL 之外的所有记录的输出 −

+-------+
| Name  |
+-------+
| John  |
| Larry |
| Bob   |
| David |
+-------+
4 rows in set (0.00 sec)

相关文章


有用资源