MySQL命令行工具:如何找出受DELETE影响的行数?
您可以在最后使用row_count()。让我们首先创建一个表-
mysql> create table rowAfftectedByDeleteDemo -> ( -> CustomerId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> CustomerName varchar(20) -> );
使用insert命令在表中插入一些记录。查询如下-
mysql> insert into rowAfftectedByDeleteDemo(CustomerName) values('John'); mysql> insert into rowAfftectedByDeleteDemo(CustomerName) values('Carol'); mysql> insert into rowAfftectedByDeleteDemo(CustomerName) values('Bob'); mysql> insert into rowAfftectedByDeleteDemo(CustomerName) values('Sam'); mysql> insert into rowAfftectedByDeleteDemo(CustomerName) values('David'); mysql> insert into rowAfftectedByDeleteDemo(CustomerName) values('Maxwell'); mysql> insert into rowAfftectedByDeleteDemo(CustomerName) values('Ramit'); mysql> insert into rowAfftectedByDeleteDemo(CustomerName) values('James');
使用select语句显示表中的所有记录。查询如下-
mysql> select *from rowAfftectedByDeleteDemo;
这是输出-
+------------+--------------+ | CustomerId | CustomerName | +------------+--------------+ | 1 | John | | 2 | Carol | | 3 | Bob | | 4 | Sam | | 5 | David | | 6 | Maxwell | | 7 | Ramit | | 8 | James | +------------+--------------+ 8 rows in set (0.00 sec)
在从表中删除行之前,row_count()的值如下所示:
mysql> select row_count();
这是输出-
+-------------+ | row_count() | +-------------+ | -1 | +-------------+ 1 row in set (0.00 sec)
以下是从表中删除行的查询-
mysql> delete from rowAfftectedByDeleteDemo where CustomerId =3 || CustomerId =4 || CustomerId =5 || CustomerId =6;
从表中删除行后,row_count()的值如下所示:
mysql> select row_count();
这是输出-
+-------------+ | row_count() | +-------------+ | 4 | +-------------+ 1 row in set (0.00 sec)