如何更新 MySQL 数据库中的两列?
mysqlmysqli database更新于 2025/4/22 15:08:17
您可以使用以逗号 (,) 分隔的 SET 命令来更新两列。语法如下 −
UPDATE yourTableName SET yourColumnName1 = ’yourValue1’, yourColumnName2 = ’yourValue2’ where yourCondition;
为了理解上述语法,让我们创建一个表。创建表的查询如下 −
mysql> create table StudentInformations -> ( -> StudentId int not null auto_increment, -> StudentFirstName varchar(20), -> StudentLastName varchar(20), -> Primary Key(StudentId) -> ); Query OK, 0 rows affected (0.57 sec)
使用 insert 命令在表中插入一些记录。 查询语句如下 −
mysql> insert into StudentInformations(StudentFirstName,StudentLastName)
values('John','Smith');
Query OK, 1 row affected (0.16 sec)
mysql> insert into StudentInformations(StudentFirstName,StudentLastName)
values('Carol','Taylor');
Query OK, 1 row affected (0.17 sec)
mysql> insert into StudentInformations(StudentFirstName,StudentLastName)
values('Mike','Jones');
Query OK, 1 row affected (0.13 sec)
mysql> insert into StudentInformations(StudentFirstName,StudentLastName)
values('Sam','Williams');
Query OK, 1 row affected (0.16 sec)
mysql> insert into StudentInformations(StudentFirstName,StudentLastName)
values('Bob','Davis');
Query OK, 1 row affected (0.14 sec)
mysql> insert into StudentInformations(StudentFirstName,StudentLastName)
values('David','Miller');
Query OK, 1 row affected (0.20 sec)
使用 select 语句显示表中的所有记录。查询如下 −
mysql> select *from StudentInformations;
以下是输出。
+-----------+------------------+-----------------+ | StudentId | StudentFirstName | StudentLastName | +-----------+------------------+-----------------+ | 1 | John | Smith | | 2 | Carol | Taylor | | 3 | Mike | Jones | | 4 | Sam | Williams | | 5 | Bob | Davis | | 6 | David | Miller | +-----------+------------------+-----------------+ 6 rows in set (0.00 sec)
以下是更新 MySQL 数据库中两列的查询。我们正在更新 ID 为 3 − 的学生记录
mysql> update StudentInformations set StudentFirstName = 'Robert', StudentLastName = 'Brown' where -> StudentId = 3; Query OK, 1 row affected (0.12 sec) Rows matched − 1 Changed − 1 Warnings − 0
使用 select 语句检查表中的更新值。查询如下−
mysql> select *from StudentInformations;
以下是输出 −
+-----------+------------------+-----------------+ | StudentId | StudentFirstName | StudentLastName | +-----------+------------------+-----------------+ | 1 | John | Smith | | 2 | Carol | Taylor | | 3 | Robert | Brown | | 4 | Sam | Williams | | 5 | Bob | Davis | | 6 | David | Miller | +-----------+------------------+-----------------+ 6 rows in set (0.00 sec)
现在,您可以在上面看到,StudentId 3条记录,即StudentFirstName和StudentLastName值已成功更改。
相关文章
有用资源
mysql 参考教程 - 该教程包含有关 mysql 的更多信息:https://www.cainiaomax.com/mysql/

