在MysqL中,可以利用delete语句配合“NULL”删除空的数据,该语句用于删除表中的数据记录,“NULL”用于表示数据为空,语法为“delete from 表名 where 字段名=' ' OR 字段名 IS NULL;”。
本教程操作环境:windows10系统、MysqL8.0.22版本、Dell G3电脑。
MysqL怎么删除空的数据
语法如下
delete from yourTableName where yourColumnName=' ' OR yourColumnName IS NULL;
上面的语法将删除空白行和NULL行。
MysqL> create table deleteRowDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> StudentName varchar(20) -> );
使用insert命令在表中插入一些记录。
查询如下
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语句显示表中的所有记录。
查询如下
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)
MysqL> delete from deleteRowDemo where StudentName='' OR StudentName IS NULL;
现在,让我们再次检查表记录。
查询如下
MysqL> select *from deleteRowDemo;
以下是输出
+----+-------------+ | Id | StudentName | +----+-------------+ | 1 | John | | 5 | Carol | | 6 | Bob | | 8 | David | +----+-------------+ 4 rows in set (0.00 sec)
推荐学习:mysql视频教程
原文地址:https://www.jb51.cc/mysql/2915293.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。