MySQL查询删除列中的空记录?
要删除列中的NULL记录,可以使用delete命令。以下是语法-
delete from yourTableName where yourColumnName IS NULL;
让我们首先创建一个表-
create table removeNullRecordsDemo -> ( -> Name varchar(100) -> );
以下是使用insert命令在表中插入记录的查询-
insert into removeNullRecordsDemo values('John'); insert into removeNullRecordsDemo values(null); insert into removeNullRecordsDemo values('Larry'); insert into removeNullRecordsDemo values('Bob'); insert into removeNullRecordsDemo values(null); insert into removeNullRecordsDemo values('David'); insert into removeNullRecordsDemo values(null);
以下是使用select语句显示表中所有记录的查询-
select *from removeNullRecordsDemo;
这将产生以下输出-
+-------+ | Name | +-------+ | John | | NULL | | Larry | | Bob | | NULL | | David | | NULL | +-------+ 7 rows in set (0.00 sec)
现在让我们删除以上列中的NULL记录-
delete from removeNullRecordsDemo where Name IS NULL;
检查空记录是否已从列中删除-
select * from removeNullRecordsDemo;
以下是显示除NULL以外的所有记录的输出-
+-------+ | Name | +-------+ | John | | Larry | | Bob | | David | +-------+ 4 rows in set (0.00 sec)