- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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)
Advertisements