Update MySQL table column by matching date using date() function?


Following is the syntax to match date with date() function and updating a column −

update yourTableName set yourColumnName=yourValue where date(yourColumnName)=curdate();

Let us first create a table −

mysql> create table DemoTable1816
     (
     Name varchar(20),
     JoiningDate datetime
     );
Query OK, 0 rows affected (0.00 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1816 values('Chris','2019-11-29 12:34:50');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1816 values('David','2019-11-30 11:00:00');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1816 values('Mike','2018-11-30 10:20:30');
Query OK, 1 row affected (0.00 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1816;

This will produce the following output −

+-------+---------------------+
| Name  | JoiningDate         |
+-------+---------------------+
| Chris | 2019-11-29 12:34:50 |
| David | 2019-11-30 11:00:00 |
| Mike  | 2018-11-30 10:20:30 |
+-------+---------------------+
3 rows in set (0.00 sec)

Here is the query to update a column by matching date −

mysql> update DemoTable1816 set Name='Robert' where date(JoiningDate)=curdate();
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1 Warnings: 0

Let us check the table records once again −

mysql> select * from DemoTable1816;

This will produce the following output −

+--------+---------------------+
| Name   | JoiningDate         |
+--------+---------------------+
| Chris  | 2019-11-29 12:34:50 |
| Robert | 2019-11-30 11:00:00 |
| Mike   | 2018-11-30 10:20:30 |
+--------+---------------------+
3 rows in set (0.00 sec)

Updated on: 25-Feb-2020

346 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements