在mysql中,可以利用delete語句配合“NULL”刪除空的數(shù)據(jù),該語句用于刪除表中的數(shù)據(jù)記錄,“NULL”用于表示數(shù)據(jù)為空,語法為“delete from 表名 where 字段名=' ' OR 字段名 IS NULL;”。
本教程操作環(huán)境:windows10系統(tǒng)、mysql8.0.22版本、Dell G3電腦。
mysql怎么刪除空的數(shù)據(jù)
使用delete命令刪除MySQL中的空白行。
語法如下
delete from yourTableName where yourColumnName=' ' OR yourColumnName IS NULL;
上面的語法將刪除空白行和NULL行。
為了理解這個概念,讓我們創(chuàng)建一個表。創(chuàng)建表的查詢?nèi)缦?/p>
mysql> create table deleteRowDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> StudentName varchar(20) -> );
使用insert命令在表中插入一些記錄。
查詢?nèi)缦?/p>
mysql> insert into deleteRowDemo(StudentName) values('John'); mysql> insert into deleteRowDemo(StudentName) values(''); mysql> insert into deleteRowDemo(StudentName) values(''); mysql> insert into deleteRowDemo(StudentName) values(NULL); mysql> insert into deleteRowDemo(StudentName) values('Carol'); mysql> insert into deleteRowDemo(StudentName) values('Bob'); mysql> insert into deleteRowDemo(StudentName) values(''); mysql> insert into deleteRowDemo(StudentName) values('David');
使用select語句顯示表中的所有記錄。
查詢?nèi)缦?/p>
mysql> select *from deleteRowDemo;
以下是輸出
+----+-------------+ | Id | StudentName | +----+-------------+ | 1 | John | | 2 | | | 3 | | | 4 | NULL | | 5 | Carol | | 6 | Bob | | 7 | | | 8 | David | +----+-------------+ 8 rows in set (0.00 sec)
這是刪除空白行以及NULL的查詢
mysql> delete from deleteRowDemo where StudentName='' OR StudentName IS NULL;
現(xiàn)在,讓我們再次檢查表記錄。
查詢?nèi)缦?/p>
mysql> select *from deleteRowDemo;
以下是輸出
+----+-------------+ | Id | StudentName | +----+-------------+ | 1 | John | | 5 | Carol | | 6 | Bob | | 8 | David | +----+-------------+ 4 rows in set (0.00 sec)
推薦學習:mysql視頻教程