Update with multiple values in MySQL WHERE clause


Let us first create a table −

mysql> create table DemoTable
   -> (
   -> Id int,
   -> Name varchar(20),
   -> Age int,
   -> CountryName varchar(10)
   -> );
Query OK, 0 rows affected (0.81 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values(100,'Chris',34,'AUS');
Query OK, 1 row affected (0.21 sec)
mysql> insert into DemoTable values(101,'Chris',31,'US');
Query OK, 1 row affected (0.21 sec)
mysql> insert into DemoTable values(102,'David',25,'UK');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable values(103,'Carol',28,'AUS');
Query OK, 1 row affected (0.21 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable;

This will produce the following output −

+------+-------+------+-------------+
| Id   | Name  | Age  | CountryName |
+------+-------+------+-------------+
|  100 | Chris |   34 | AUS         |
|  101 | Chris |   31 | US          |
|  102 | David |   25 | UK          |
|  103 | Carol |   28 | AUS         |
+------+-------+------+-------------+
4 rows in set (0.00 sec)

Here is the query to update with multiple values in WHERE clause −

mysql> update DemoTable
   -> set Name='Robert'
   -> where Age=31 and CountryName='US';
Query OK, 1 row affected (0.44 sec)
Rows matched: 1 Changed: 1 Warnings: 0

Let us check the table records once again −

mysql> select * from DemoTable;

This will produce the following output −

+------+--------+------+-------------+
| Id   | Name   | Age  | CountryName |
+------+--------+------+-------------+
|  100 | Chris  |   34 | AUS         |
|  101 | Robert |   31 | US          |
|  102 | David  |   25 | UK          |  
|  103 | Carol  |   28 | AUS         |
+------+--------+------+-------------+
4 rows in set (0.00 sec)

Updated on: 28-Feb-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements