要更新MySQL中的一系列记录,可以使用BETWEEN。让我们首先创建一个表:
create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Name varchar(20), Age int );
以下是使用insert命令在表中插入一些记录的查询:
insert into DemoTable(Name,Age) values('Larry',23); insert into DemoTable(Name,Age) values('Sam',24); insert into DemoTable(Name,Age) values('Chris',21); insert into DemoTable(Name,Age) values('Carol',25); insert into DemoTable(Name,Age) values('David',22); insert into DemoTable(Name,Age) values('Robert',26); insert into DemoTable(Name,Age) values('John',20); insert into DemoTable(Name,Age) values('Mike',27); insert into DemoTable(Name,Age) values('Johnny',28); insert into DemoTable(Name,Age) values('James',23);
以下是使用select命令显示表中记录的查询:
select *from DemoTable;
这将产生以下输出:
+----+--------+------+ | Id | Name | Age | +----+--------+------+ | 1 | Larry | 23 | | 2 | Sam | 24 | | 3 | Chris | 21 | | 4 | Carol | 25 | | 5 | David | 22 | | 6 | Robert | 26 | | 7 | John | 20 | | 8 | Mike | 27 | | 9 | Johnny | 28 | | 10 | James | 23 | +----+--------+------+ 10 rows in set (0.00 sec)
以下是查询以更新MySQL中的一系列记录。我们将ID的名称更新为“ Bob”,范围为5到10:
update DemoTable set Name='Bob', Age=23 where Id between 5 AND 10; Rows matched: 6 Changed: 6 Warnings: 0
现在让我们显示所有记录,包括更新的记录:
select *from DemoTable;
这将产生以下输出
+----+-------+------+ | Id | Name | Age | +----+-------+------+ | 1 | Larry | 23 | | 2 | Sam | 24 | | 3 | Chris | 21 | | 4 | Carol | 25 | | 5 | Bob | 23 | | 6 | Bob | 23 | | 7 | Bob | 23 | | 8 | Bob | 23 | | 9 | Bob | 23 | | 10 | Bob | 23 | +----+-------+------+ 10 rows in set (0.00 sec)