如何在指定列中刪除空記錄?
要刪除列中的空記錄,可以使用 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)
下面,我們在上面的列中刪除空記錄 −
mysql> delete from removeNullRecordsDemo where Name IS NULL; Query OK, 3 rows affected (0.16 sec)
檢視是否已從列中刪除空記錄 −
mysql> select * from removeNullRecordsDemo;
以下輸出顯示所有記錄,不含空記錄 −
+-------+ | Name | +-------+ | John | | Larry | | Bob | | David | +-------+ 4 rows in set (0.00 sec)
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP