

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Counting voucher value total since the beginning of the month and year in MySQL
For this, use MySQL MONTH() and YEAR() methods. Let us first create a table −
mysql> create table DemoTable1562 -> ( -> VoucherValue int, -> RechargeDate date -> ); Query OK, 0 rows affected (1.40 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1562 values(149,'2019-10-21'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1562 values(199,'2019-10-13'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable1562 values(399,'2018-10-13'); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable1562 values(450,'2019-10-13'); Query OK, 1 row affected (0.20 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1562;
This will produce the following output −
+--------------+--------------+ | VoucherValue | RechargeDate | +--------------+--------------+ | 149 | 2019-10-21 | | 199 | 2019-10-13 | | 399 | 2018-10-13 | | 450 | 2019-10-13 | +--------------+--------------+ 4 rows in set (0.00 sec)
The current date is as follows −
mysql> select curdate(); +------------+ | curdate() | +------------+ | 2019-10-13 | +------------+ 1 row in set (0.00 sec)
Here is the query to count voucher value total since the beginning of month and year.
mysql> select sum(VoucherValue) from DemoTable1562 -> where month(RechargeDate)=month(curdate()) -> and year(RechargeDate)=year(curdate());
This will produce the following output −
+-------------------+ | sum(VoucherValue) | +-------------------+ | 798 | +-------------------+ 1 row in set (0.00 sec)
- Related Questions & Answers
- Group month and year in MySQL?
- Filter the records of current day, month and year in MySQL?
- Counting the total and true condition values in a MySQL table?
- Extract the month and year in the following format: “mm-yyyy” (month year) along with all columns in MySQL?
- Java Program to get the seconds since the beginning of the Java epoch
- MySQL datatype to store month and year only?
- How can we extract the Year and Month from a date in MySQL?
- MySQL query to fetch date with year and month?
- Format MySQL date and convert to year-month-day
- Get beginning and end date from a specific year in MySQL
- Python Pandas - Get the month of the year component of the Period
- Extract the Day / Month / Year from a Timestamp in PHP MySQL?
- How to convert year, month, and day of the month into a complete date in R?
- How to select month and year from dates in MySQL?
- Do a select in MySQL based only on month and year?
Advertisements