Perform MySQL UPDATE on the basis of DATE value in another column


Let us first create a table −

mysql> create table DemoTable(
   Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   Name varchar(100),
   AdmissionDate date
);
Query OK, 0 rows affected (0.42 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable(Name,AdmissionDate) values('Chris','2019-11-21');
Query OK, 1 row affected (0.19 sec)
mysql> insert into DemoTable(Name,AdmissionDate) values('Mike','2019-03-11');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable(Name,AdmissionDate) values('Sam','2018-04-01');
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable(Name,AdmissionDate) values('Carol','2019-05-01');
Query OK, 1 row affected (0.40 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+----+-------+---------------+
| Id | Name  | AdmissionDate |
+----+-------+---------------+
|  1 | Chris | 2019-11-21    |
|  2 | Mike  | 2019-03-11    |
|  3 | Sam   | 2018-04-01    |
|  4 | Carol | 2019-05-01    |
+----+-------+---------------+
4 rows in set (0.00 sec)

Let us now update the table on the basis of a specific DATE value i.e. 2019-04-01 here −

mysql> update DemoTable set Name='Robert',AdmissionDate='2019-04-01' where Id=3;
Query OK, 1 row affected (0.14 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   | AdmissionDate |
+----+--------+---------------+
|  1 | Chris  | 2019-11-21    |
|  2 | Mike   | 2019-03-11    |
|  3 | Robert | 2019-04-01    |
|  4 | Carol  | 2019-05-01    |
+----+--------+---------------+
4 rows in set (0.00 sec)

Updated on: 24-Sep-2019

131 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements