
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Select total from a MySQL table based on month
For this, you can use GROUP BY MONTH(). Let us first create a table −
mysql> create table DemoTable1628 -> ( -> PurchaseDate date, -> Amount int -> ); Query OK, 0 rows affected (1.55 sec)
Insert some records in the table using insert command.
mysql> insert into DemoTable1628 values('2019-01-10',1500); Query OK, 1 row affected (0.68 sec) mysql> insert into DemoTable1628 values('2019-10-10',2000); Query OK, 1 row affected (0.61 sec) mysql> insert into DemoTable1628 values('2019-10-24',100); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable1628 values('2019-11-10',500); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1628 values('2019-12-10',1600); Query OK, 1 row affected (0.32 sec) mysql> insert into DemoTable1628 values('2019-10-10',900); Query OK, 1 row affected (0.47 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1628;
This will produce the following output −
+--------------+--------+ | PurchaseDate | Amount | +--------------+--------+ | 2019-01-10 | 1500 | | 2019-10-10 | 2000 | | 2019-10-24 | 100 | | 2019-11-10 | 500 | | 2019-12-10 | 1600 | | 2019-10-10 | 900 | +--------------+--------+ 6 rows in set (0.00 sec)
Here is the query to get totals from database table based on month −
mysql> select month(PurchaseDate) as Month,year(PurchaseDate) as Year,SUM(Amount) -> from DemoTable1628 -> group by month(PurchaseDate);
This will produce the following output −
+-------+------+-------------+ | Month | Year | SUM(Amount) | +-------+------+-------------+ | 1 | 2019 | 1500 | | 10 | 2019 | 3000 | | 11 | 2019 | 500 | | 12 | 2019 | 1600 | +-------+------+-------------+ 4 rows in set (0.20 sec)
- Related Articles
- Selecting data from a MySQL table based on a specific month?
- Do a select in MySQL based only on month and year?
- MySQL query to select records from a table on the basis of a particular month number?
- Select and filter the records on month basis in a MySQL table?
- How can you select data from a table based on some criteria using MySQL in Python?
- Selecting from a MySQL table based on parts of a timestamp?
- MySQL query to select all entries from a particular month
- MySQL to fetch records based on a specific month and year?
- Delete only some rows from a table based on a condition in MySQL
- Select records from a table on the basis of keywords in MySQL
- Update a table based on StudentId value in MySQL?
- Select rows from a Pandas DataFrame based on column values
- Insert values in a table by MySQL SELECT from another table in MySQL?
- How to select month and year from dates in MySQL?
- MySQL select query to select rows from a table that are not in another table?

Advertisements