使用 MySQL DELETE 查询删除选择性多条记录
mysqlmysqli database
对于选择性多条记录,请使用 MySQL IN()。要删除它们,请使用 MySQL DELETE。让我们首先创建一个表 −
mysql> create table DemoTable ( ClientId varchar(40), ClientName varchar(50) ); Query OK, 0 rows affected (0.62 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable values('CLI-101','Chris'); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable values('CLI-110','Adam'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values('CLI-220','Mike'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('CLI-120','Bob'); Query OK, 1 row affected (0.53 sec) mysql> insert into DemoTable values('CLI-240','Sam'); Query OK, 1 row affected (0.06 sec)
使用 select 语句显示表中的所有记录 −
mysql> select *from DemoTable;
这将产生以下输出 −
+----------+------------+ | ClientId | ClientName | +----------+------------+ | CLI-101 | Chris | | CLI-110 | Adam | | CLI-220 | Mike | | CLI-120 | Bob | | CLI-240 | Sam | +----------+------------+ 5 rows in set (0.00 sec)
这是删除选择性多条记录的查询 −
mysql> delete from DemoTable where ClientId IN('CLI-101','CLI-220','CLI-240'); Query OK, 3 rows affected (0.10 sec)
让我们再次检查表记录 −
mysql> select *from DemoTable;
这将产生以下输出 −
+----------+------------+ | ClientId | ClientName | +----------+------------+ | CLI-110 | Adam | | CLI-120 | Bob | +----------+------------+ 2 rows in set (0.00 sec)