使用日期格式(例如“ 10/12/2010”)更新具有特定年份的表中的记录?

要更新特定年份的记录,请使用YEAR()以下语法中的方法:

update yourTableName set yourColumnName1=yourValue1 where YEAR(str_to_date(yourColumnName2,'%d/%m/%Y'))=yourValue2;

让我们首先创建一个表-

mysql> create table DemoTable1924
   (
   UserName varchar(20),
   UserJoiningDate varchar(40)
   );

使用插入命令在表中插入一些记录-

mysql> insert into DemoTable1924 values('Chris','10/12/2010');
mysql> insert into DemoTable1924 values('David','20/01/2011');
mysql> insert into DemoTable1924 values('Mike','20/01/2010');
mysql> insert into DemoTable1924 values('Carol','26/04/2013');

使用select语句显示表中的所有记录-

mysql> select * from DemoTable1924;

这将产生以下输出-

+----------+-----------------+
| UserName | UserJoiningDate |
+----------+-----------------+
| Chris    | 10/12/2010      |
| David    | 20/01/2011      |
| Mike     | 20/01/2010      |
| Carol    | 26/04/2013      |
+----------+-----------------+
4 rows in set (0.00 sec)

这是根据特定年份更新记录的查询-

mysql> update DemoTable1924 set UserName='Robert' where YEAR(str_to_date(UserJoiningDate,'%d/%m/%Y'))=2010;
Rows matched: 2  Changed: 2 Warnings: 0

让我们再次检查表记录-

mysql> select * from DemoTable1924;

这将产生以下输出-

+----------+-----------------+
| UserName | UserJoiningDate |
+----------+-----------------+
| Robert   | 10/12/2010      |
| David    | 20/01/2011      |
| Robert   | 20/01/2010      |
| Carol    | 26/04/2013      |
+----------+-----------------+
4 rows in set (0.00 sec)