通过使用NOT IN排除未删除的记录,从MySQL表中删除记录
让我们首先创建一个表-
mysql> create table DemoTable ( FirstName varchar(100) );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values('Chris'); mysql> insert into DemoTable values('Robert'); mysql> insert into DemoTable values('Mike'); mysql> insert into DemoTable values('Sam'); mysql> insert into DemoTable values('David');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+-----------+ | FirstName | +-----------+ | Chris | | Robert | | Mike | | Sam | | David | +-----------+ 5 rows in set (0.00 sec)
以下是通过使用NOTIN排除未删除的记录来从MySQL表删除记录的查询-
mysql> delete from DemoTable where FirstName NOT IN('Robert', 'Mike');
让我们再次检查表记录-
mysql> select *from DemoTable;
这将产生以下输出-
+-----------+ | FirstName | +-----------+ | Robert | | Mike | +-----------+ 2 rows in set (0.00 sec)