如何在 MySQL 中批量更新某特定字段的所有记录?
我们首先创建一个演示表 -
mysql> create table BulkChangeDemo -> ( -> CustomerId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> CustomerName varchar(20), -> isEducated boolean -> ); Query OK, 0 rows affected (1.47 sec)
使用以下命令使用 insert 命令在表中插入一些记录。查询如下 -
mysql> insert into BulkChangeDemo(CustomerName,isEducated) values('Larry',true); Query OK, 1 row affected (0.09 sec) mysql> insert into BulkChangeDemo(CustomerName,isEducated) values('John',false); Query OK, 1 row affected (0.16 sec) mysql> insert into BulkChangeDemo(CustomerName,isEducated) values('Carol',false); Query OK, 1 row affected (0.25 sec) mysql> insert into BulkChangeDemo(CustomerName,isEducated) values('Mike',true); Query OK, 1 row affected (0.18 sec) mysql> insert into BulkChangeDemo(CustomerName,isEducated) values('David',false); Query OK, 1 row affected (0.13 sec) mysql> insert into BulkChangeDemo(CustomerName,isEducated) values('Bob',false); Query OK, 1 row affected (0.10 sec)
使用 select 语句从表中显示所有记录。查询如下 -
mysql> select *from BulkChangeDemo;
以下是输出 -
+------------+--------------+------------+ | CustomerId | CustomerName | isEducated | +------------+--------------+------------+ | 1 | Larry | 1 | | 2 | John | 0 | | 3 | Carol | 0 | | 4 | Mike | 1 | | 5 | David | 0 | | 6 | Bob | 0 | +------------+--------------+------------+ 6 rows in set (0.00 sec)
以下是针对某特定字段批量更新所有记录的查询 -
mysql> update BulkChangeDemo set isEducated =true; Query OK, 4 rows affected (0.18 sec) Rows matched: 6 Changed: 4 Warnings: 0
广告