在MySQL WHERE子句中获取当前年份?
要获取当前年份,请YEAR()
与一起使用CURDATE()
。让我们首先创建一个表-
mysql> create table DemoTable1360 -> ( -> JoiningYear int -> ) -> ;
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1360 values(1998); mysql> insert into DemoTable1360 values(2018); mysql> insert into DemoTable1360 values(2016); mysql> insert into DemoTable1360 values(2019); mysql> insert into DemoTable1360 values(2017); mysql> insert into DemoTable1360 values(2015);
使用select语句显示表中的所有记录-
mysql> select * from DemoTable1360;
这将产生以下输出-
+-------------+ | JoiningYear | +-------------+ | 1998 | | 2018 | | 2016 | | 2019 | | 2017 | | 2015 | +-------------+ 6 rows in set (0.00 sec)
以下是在MySQL中获取当前年份的查询,其中子句-
mysql> select * from DemoTable1360 where JoiningYear=year(curdate());
这将产生以下输出-
+-------------+ | JoiningYear | +-------------+ | 2019 | +-------------+ 1 row in set (0.00 sec)