To sum current month records, use the SUM() and MONTH() function. Let us first create a table −
mysql> create table DemoTable1889 ( DueDate date, Amount int ); Query OK, 0 rows affected (0.00 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1889 values('2019-12-11',500); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1889 values('2019-11-11',1000); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1889 values('2018-12-04',700); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1889 values('2017-12-10',300); Query OK, 1 row affected (0.00 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1889;
This will produce the following output −
+------------+--------+ | 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)
The current date is as follows −
mysql> select curdate(); +------------+ | curdate() | +------------+ | 2019-12-10 | +------------+ 1 row in set (0.00 sec)
Here is the query to sum current month records in MySQL −
mysql> select sum(Amount) from DemoTable1889 where MONTH(DueDate)=MONTH(curdate());
This will produce the following output −
+-------------+ | sum(Amount) | +-------------+ | 1500 | +-------------+ 1 row in set (0.00 sec)