在MySQL中使用ID从表中删除多行?
您可以使用IN语句在MySQL中使用id从表中删除多行。语法如下-
delete from yourTableName where yourColumnName in(value1,value2,.....valueN);
为了理解上述语法,让我们创建一个表。以下是创建表的查询。
mysql> create table DeleteManyRows −> ( −> Id int, −> Name varchar(200), −> Age int −> );
借助insert命令将一些记录插入表中。查询如下-
mysql> insert into DeleteManyRows values(1,'John',23); mysql> insert into DeleteManyRows values(2,'Johnson',22); mysql> insert into DeleteManyRows values(3,'Sam',20); mysql> insert into DeleteManyRows values(4,'David',26); mysql> insert into DeleteManyRows values(5,'Carol',21); mysql> insert into DeleteManyRows values(6,'Smith',29);
在select语句的帮助下显示表中的所有记录。查询如下-
mysql> select *from DeleteManyRows;
以下是输出-
+------+---------+------+ | Id | Name | Age | +------+---------+------+ | 1 | John | 23 | | 2 | Johnson | 22 | | 3 | Sam | 20 | | 4 | David | 26 | | 5 | Carol | 21 | | 6 | Smith | 29 | +------+---------+------+ 6 rows in set (0.00 sec)
这是在IN语句的帮助下从表中删除行的查询。查询如下-
mysql> delete from DeleteManyRows where Id in(1,2,3,4);
让我们检查一下删除1,2,3,4之类的多行后现在有多少行。查询如下-
mysql> select *from DeleteManyRows;
以下是输出-
+------+-------+------+ | Id | Name | Age | +------+-------+------+ | 5 | Carol | 21 | | 6 | Smith | 29 | +------+-------+------+ 2 rows in set (0.00 sec)