如何更新MySQL数据库中的两列?
您可以使用用逗号(,)分隔的SET命令更新两列。语法如下-
UPDATE yourTableName SET yourColumnName1 = ’yourValue1’, yourColumnName2 = ’yourValue2’ where yourCondition;
为了理解上述语法,让我们创建一个表。创建表的查询如下-
create table StudentInformations-> (
-> StudentId int not null auto_increment,
-> StudentFirstName varchar(20),
-> StudentLastName varchar(20),
-> Primary Key(StudentId)
-> );
使用insert命令在表中插入一些记录。查询如下-
insert into StudentInformations(StudentFirstName,StudentLastName)values('John','Smith');
insert into StudentInformations(StudentFirstName,StudentLastName)
values('Carol','Taylor');
insert into StudentInformations(StudentFirstName,StudentLastName)
values('Mike','Jones');
insert into StudentInformations(StudentFirstName,StudentLastName)
values('Sam','Williams');
insert into StudentInformations(StudentFirstName,StudentLastName)
values('Bob','Davis');
insert into StudentInformations(StudentFirstName,StudentLastName)
values('David','Miller');
使用select语句显示表中的所有记录。查询如下-
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的学生的记录-
update StudentInformations set StudentFirstName = 'Robert',StudentLastName = 'Brown' where
-> StudentId = 3;
Rows matched − 1 Changed − 1 Warnings − 0
使用select语句检查表中的更新值。查询如下-
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数据库中的两列? 的全部内容, 来源链接: utcz.com/z/327244.html