如何为 MySQL 中前三个列值设置特定值?
若要仅为前三个值设置特定值,你需要使用 LIMIT 3。我们首先创建一个表 -
mysql> create table DemoTable1968 ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Name varchar(20) ); Query OK, 0 rows affected (0.00 sec)
使用 insert 命令在表中插入一些记录 -
mysql> insert into DemoTable1968(Name) values('Chris'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1968(Name) values('David'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1968(Name) values('Sam'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1968(Name) values('Mike'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1968(Name) values('Carol'); Query OK, 1 row affected (0.00 sec)
使用 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; Query OK, 3 rows affected (0.00 sec) 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)
广告