MySQL搜索结果按月格式2015-07-01 11:15:30?
为此使用MONTH()
和YEAR()
。让我们首先创建一个表-
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, ShippingDate datetime );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable(ShippingDate) values('2019-01-21 10:40:21'); mysql> insert into DemoTable(ShippingDate) values('2015-07-01 11:15:30'); mysql> insert into DemoTable(ShippingDate) values('2012-12-31 10:45:56');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+----+---------------------+ | Id | ShippingDate | +----+---------------------+ | 1 | 2019-01-21 10:40:21 | | 2 | 2015-07-01 10:40:20 | | 3 | 2012-12-31 10:45:56 | +----+---------------------+ 3 rows in set (0.00 sec)
以下是按MONTH和YEAR搜索结果的查询-
mysql> select *from DemoTable where MONTH(ShippingDate)=7 and YEAR(ShippingDate)=2015;
这将产生以下输出-
+----+---------------------+ | Id | ShippingDate | +----+---------------------+ | 2 | 2015-07-01 11:15:30 | +----+---------------------+ 1 row in set (0.03 sec)