MySQL 根据特定月份和年份获取记录?
mysqlmysqli database
为此,使用 MONTH() 和 YEAR()。让我们首先创建一个表 −
mysql> create table DemoTable1846 ( PurchaseDate date ); Query OK, 0 rows affected (0.00 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable1846 values('2019-01-10'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1846 values('2019-12-24'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1846 values('2018-09-21'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1846 values('2017-10-26'); Query OK, 1 row affected (0.00 sec)
使用 select 语句显示表中的所有记录 −
mysql> select * from DemoTable1846;
这将产生以下输出 −
+--------------+ | PurchaseDate | +--------------+ | 2019-01-10 | | 2019-12-24 | | 2018-09-21 | | 2017-10-26 | +--------------+ 4 rows in set (0.00 sec)
以下是根据特定月份和年份获取记录的查询
mysql> select * from DemoTable1846 where month(PurchaseDate)=12 and year(PurchaseDate)=2019;
这将产生以下输出 −
+--------------+ | PurchaseDate | +--------------+ | 2019-12-24 | +--------------+ 1 row in set (0.00 sec)