MySQL 命令行工具:如何找到受 DELETE 影响的行数?


为此,你可以在末尾使用 row_count()。我们首先创建一个表 -

mysql> create table rowAfftectedByDeleteDemo
   -> (
   -> CustomerId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> CustomerName varchar(20)
   -> );
Query OK, 0 rows affected (0.86 sec)

使用 insert 命令在表中插入一些记录。查询如下 -

mysql> insert into rowAfftectedByDeleteDemo(CustomerName) values('John');
Query OK, 1 row affected (0.14 sec)
mysql> insert into rowAfftectedByDeleteDemo(CustomerName) values('Carol');
Query OK, 1 row affected (0.10 sec)
mysql> insert into rowAfftectedByDeleteDemo(CustomerName) values('Bob');
Query OK, 1 row affected (0.09 sec)
mysql> insert into rowAfftectedByDeleteDemo(CustomerName) values('Sam');
Query OK, 1 row affected (0.09 sec)
mysql> insert into rowAfftectedByDeleteDemo(CustomerName) values('David');
Query OK, 1 row affected (0.08 sec)
mysql> insert into rowAfftectedByDeleteDemo(CustomerName) values('Maxwell');
Query OK, 1 row affected (0.09 sec)
mysql> insert into rowAfftectedByDeleteDemo(CustomerName) values('Ramit');
Query OK, 1 row affected (0.08 sec)
mysql> insert into rowAfftectedByDeleteDemo(CustomerName) values('James');
Query OK, 1 row affected (0.10 sec)

使用 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;
Query OK, 4 rows affected (0.13 sec)

从表中删除行后,row_count() 的值为如下 -

mysql> select row_count();

以下是输出 -

+-------------+
| row_count() |
+-------------+
| 4           |
+-------------+
1 row in set (0.00 sec)

更新日期: 2019-07-30

545 次浏览

开启您的 职业生涯

通过完成课程获得认证

开始
广告