用“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) ); Query OK, 0 rows affected (0.00 sec)
使用 insert 命令在表中插入一些记录 -
mysql> insert into DemoTable1924 values('Chris','10/12/2010'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1924 values('David','20/01/2011'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1924 values('Mike','20/01/2010'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1924 values('Carol','26/04/2013'); Query OK, 1 row affected (0.00 sec)
使用 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; Query OK, 2 rows affected (0.00 sec) 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)
广告