MySQL 根据特定月份和年份获取记录?
mysqlmysqli database更新于 2024/10/13 17:48:17
为此,使用 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)
相关文章
有用资源
mysql 参考教程 - 该教程包含有关 mysql 的更多信息:https://www.cainiaomax.com/mysql/

