如何按月用 MySQL 进行 SELECT?
如需按月进行选择,请使用 MONTH() 函数。我们首先创建一个表 -
mysql> create table DemoTable1599 -> ( -> Shippingdate datetime -> ); Query OK, 0 rows affected (0.78 sec)
使用 insert 命令在表中插入一些记录 -
mysql> insert into DemoTable1599 values('2019-10-21'); Query OK, 1 row affected (0.28 sec) mysql> insert into DemoTable1599 values('2018-12-12'); Query OK, 1 row affected (0.35 sec) mysql> insert into DemoTable1599 values('2015-11-21'); Query OK, 1 row affected (0.34 sec) mysql> insert into DemoTable1599 values('2017-12-31'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable1599 values('2018-12-26'); Query OK, 1 row affected (0.17 sec)
使用 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)
以下是按月进行 SELECT 的查询 -
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)
广告