从MySQL表中删除多个条目
要从MySQL表中删除多个记录,请使用JOIN。让我们首先创建一个表−
mysql> create table DemoTabl(Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, FirstName varchar(100));
Query OK, 0 rows affected (0.69 sec)
使用insert命令在表中插入一些记录−
mysql> insert into DemoTable(FirstName) values('Chris');
Query OK, 1 row affected (0.19 sec)
mysql> insert into DemoTable(FirstName) values('Bob');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable(FirstName) values('Mike');
Query OK, 1 row affected (0.28 sec)
mysql> insert into DemoTable(FirstName) values('Adam');
Query OK, 1 row affected (0.33 sec)
mysql> insert into DemoTable(FirstName) values('Bob');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable(FirstName) values('Adam');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable(FirstName) values('Bob');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable(FirstName) values('Mike');
Query OK, 1 row affected (0.23 sec)
使用select语句显示表中的所有记录−
mysql> select *from DemoTable;
这将产生如下输出−
+----+-----------+
| Id | FirstName |
+----+-----------+
| 1 | Chris |
| 2 | Bob |
| 3 | Mike |
| 4 | Adam |
| 5 | Bob |
| 6 | Adam |
| 7 | Bob |
| 8 | Mike |
+----+-----------+
8 rows in set (0.00 sec)
以下是删除多个条目的查询−
mysql> delete DemoTable
from DemoTable
join DemoTable tbl on DemoTable.FirstName= tbl.FirstName
where tbl.FirstName = 'Bob';
Query OK, 3 rows affected (0.15 sec)
让我们再次检查表记录−
mysql> select *from DemoTable;
这将产生如下输出−
+----+-----------+
| Id | FirstName |
+----+-----------+
| 1 | Chris |
| 3 | Mike |
| 4 | Adam |
| 6 | Adam |
| 8 | Mike |
+----+-----------+
5 rows in set (0.00 sec)
阅读更多:MySQL 教程