要汇总当前月份记录,请使用SUM()和MONTH()函数。让我们首先创建一个表-
create table DemoTable1889 ( DueDate date, Amount int );
使用插入命令在表中插入一些记录-
insert into DemoTable1889 values('2019-12-11',500); insert into DemoTable1889 values('2019-11-11',1000); insert into DemoTable1889 values('2018-12-04',700); insert into DemoTable1889 values('2017-12-10',300);
使用select语句显示表中的所有记录-
select * from DemoTable1889;
这将产生以下输出-
+------------+--------+ | DueDate | Amount | +------------+--------+ | 2019-12-11 | 500 | | 2019-11-11 | 1000 | | 2018-12-04 | 700 | | 2017-12-10 | 300 | +------------+--------+ 4 rows in set (0.00 sec)
当前日期如下-
select curdate(); +------------+ | curdate() | +------------+ | 2019-12-10 | +------------+ 1 row in set (0.00 sec)
这是汇总MySQL中当前月份记录的查询-
select sum(Amount) from DemoTable1889 where MONTH(DueDate)=MONTH(curdate());
这将产生以下输出-
+-------------+ | sum(Amount) | +-------------+ | 1500 | +-------------+ 1 row in set (0.00 sec)