为MySQL中的前三列值设置特定的值?
要仅为前三个值设置特定值,您需要使用LIMIT3。让我们首先创建一个表-
mysql> create table DemoTable1968 ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Name varchar(20) );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1968(Name) values('Chris'); mysql> insert into DemoTable1968(Name) values('David'); mysql> insert into DemoTable1968(Name) values('Sam'); mysql> insert into DemoTable1968(Name) values('Mike'); mysql> insert into DemoTable1968(Name) values('Carol');
使用select语句显示表中的所有记录-
mysql> select * from DemoTable1968;
这将产生以下输出-
+----+-------+ | Id | Name | +----+-------+ | 1 | Chris | | 2 | David | | 3 | Sam | | 4 | Mike | | 5 | Carol | +----+-------+ 5 rows in set (0.00 sec)
这是为前三个列值设置特定值的查询-
mysql> update DemoTable1968 set Name='Robert' order by Id limit 3; Rows matched: 3 Changed: 3 Warnings: 0
让我们再次检查表记录:
mysql> select * from DemoTable1968;
这将产生以下输出-
+----+--------+ | Id | Name | +----+--------+ | 1 | Robert | | 2 | Robert | | 3 | Robert | | 4 | Mike | | 5 | Carol | +----+--------+ 5 rows in set (0.00 sec)