根据特定月份和年份从 MySQL 中提取记录?
为此,请使用 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)
广告