要使用月份选择,请使用MONTH()
方法。让我们首先创建一个表-
mysql> create table DemoTable1599 -> ( -> Shippingdate datetime -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1599 values('2019-10-21'); mysql> insert into DemoTable1599 values('2018-12-12'); mysql> insert into DemoTable1599 values('2015-11-21'); mysql> insert into DemoTable1599 values('2017-12-31'); mysql> insert into DemoTable1599 values('2018-12-26');
使用select语句显示表中的所有记录-
mysql> select * from DemoTable1599;
这将产生以下输出-
+---------------------+ | Shippingdate | +---------------------+ | 2019-10-21 00:00:00 | | 2018-12-12 00:00:00 | | 2015-11-21 00:00:00 | | 2017-12-31 00:00:00 | | 2018-12-26 00:00:00 | +---------------------+ 5 rows in set (0.00 sec)
以下是按月查询的查询-
mysql> select * from DemoTable1599 -> where month(Shippingdate)=12;
这将产生以下输出-
+---------------------+ | Shippingdate | +---------------------+ | 2018-12-12 00:00:00 | | 2017-12-31 00:00:00 | | 2018-12-26 00:00:00 | +---------------------+ 3 rows in set (0.00 sec)