Perform MySQL update with AND operator


Let us first create a table −

mysql> create table DemoTable613 (Id int,Age int,isMarried tinyint(1));
Query OK, 0 rows affected (0.54 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable613 values(100,29,0);
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable613 values(200,22,0);
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable613 values(300,30,1);
Query OK, 1 row affected (0.19 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable613;

This will produce the following output −

+------+------+-----------+
| Id   | Age  | isMarried |
+------+------+-----------+
|  100 |   29 |         0 |
|  200 |   22 |         0 |
|  300 |   30 |         1 |
+------+------+-----------+
3 rows in set (0.00 sec)

Following is the query to perform MySQL update with AND operator −

mysql> update DemoTable613 set isMarried=(Age=29 AND (Id=100));
Query OK, 2 rows affected (0.24 sec)
Rows matched: 3 Changed: 2 Warnings: 0

Let us check the table records once again −

mysql> select *from DemoTable613;

This will produce the following output −

+------+------+-----------+
| Id   | Age  | isMarried |
+------+------+-----------+
|  100 |   29 |         1 |
|  200 |   22 |         0 |
|  300 |   30 |         0 |
+------+------+-----------+
3 rows in set (0.00 sec)

Updated on: 22-Aug-2019

68 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements