How to update two columns in a MySQL database?



You can update two columns using SET command separated with comma(,). The syntax is as follows −

UPDATE yourTableName SET yourColumnName1 = ’yourValue1’, yourColumnName2 = ’yourValue2’ where yourCondition;

To understand the above syntax, let us create a table. The query to create a table is as follows −

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 some records in the table using insert command. The query is as follows −

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)

Display all records from the table using select statement. The query is as follows −

mysql> select *from StudentInformations;

The following is the output.

+-----------+------------------+-----------------+ | 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)

Here is the query to update two columns in MySQL database. We are updating the records of student with 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

Check the updated value in the table using select statement. The query is as follows −

mysql> select *from StudentInformations;

The following is the output −

+-----------+------------------+-----------------+ | 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)

Now, you can see above, the StudentId 3 records i.e. StudentFirstName and StudentLastName values have been changed successfully.

Updated on: 2020-06-30T06:32:46+05:30

567 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements