删除除MySQL中仅特定行以外的所有行?
要删除除单个特定行以外的所有行,请尝试以下语法-
delete from yourTableName where yourColumnName!=yourValue;
让我们首先创建一个表-
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Subject varchar(100) );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable(Subject) values('MySQL'); mysql> insert into DemoTable(Subject) values('MongoDB'); mysql> insert into DemoTable(Subject) values('Java');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
输出结果
+----+---------+ | Id | Subject | +----+---------+ | 1 | MySQL | | 2 | MongoDB | | 3 | Java | +----+---------+ 3 rows in set (0.00 sec)
这是删除除单个特定ow以外的所有行的查询-
mysql> delete from DemoTable where Subject!='MongoDB';
让我们再次检查表中的所有记录-
mysql> select *from DemoTable;
输出结果
+----+---------+ | Id | Subject | +----+---------+ | 2 | MongoDB | +----+---------+ 1 row in set (0.00 sec)