在 MySQL 中为前三列值设置特定值?
mysqlmysqli database更新于 2024/12/7 7:40:17
要仅为前三个值设置特定值,您需要使用 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)
相关文章
有用资源
mysql 参考教程 - 该教程包含有关 mysql 的更多信息:https://www.cainiaomax.com/mysql/

