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