对于年月日期获取,您可以在MySQL中使用YEAR()
和MONTH()
运行。让我们首先创建一个表-
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, ShippingDate datetime );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable(ShippingDate) values('2019-01-31'); mysql> insert into DemoTable(ShippingDate) values('2018-12-01'); mysql> insert into DemoTable(ShippingDate) values('2019-06-02'); mysql> insert into DemoTable(ShippingDate) values('2019-11-18');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
输出结果
+----+-----------------------+ | Id | ShippingDate | +----+-----------------------+ | 1 | 2019-01-31 00 :00 :00 | | 2 | 2018-12-01 00 :00 :00 | | 3 | 2019-06-02 00 :00 :00 | | 4 | 2019-11-18 00 :00 :00 | +----+-----------------------+ 4 rows in set (0.00 sec)
以下是根据特定年份和月份获取日期的查询-
mysql> select *from DemoTable where ( year(ShippingDate)='2019') and (month(ShippingDate)='06');
输出结果
+----+-----------------------+ | Id | ShippingDate | +----+-----------------------+ | 3 | 2019-06-02 00 :00 :00 | +----+-----------------------+ 1 row in set (0.07 sec)