Java, Python, Database, Flutter, Matlap, Micorcontroller, Tutorials, Swing Framework

Hancie e-Learning Studio

Learn Java, Learn HTML, CSS, PHP, Javascript, Python Tutorials || Download program source codes || Java Project and Source code available here || All types error troubleshooting tips available here

How to Get Records of Current Month in MySQL?

 


Obtaining current-month records in MySQL is useful for reporting and data analysis. It allows you to get sales statistics for the current month, the number of signups for the current month, and other valuable data. Here's how to use MySQL to retrieve records for the current month.

How to Get Records of Current Month in MySQL

The procedures to retrieve current month records in MySQL are shown below. Additionally, we'll use it to attract new clients this month.

Assume you have the table sales(order date, sale, orders) below, which provides the daily number of orders and sale amount.

mysql> select * from sales;
+------------+------+--------+
| order_date | sale | orders |
+------------+------+--------+
| 2020-04-28 |  300 |     10 |
| 2020-04-29 |  250 |     15 |
| 2020-04-30 |  250 |     12 |
| 2020-05-01 |  250 |     14 |
| 2020-05-02 |  150 |     20 |
| 2020-05-03 |  300 |     21 |
| 2020-05-04 |  200 |     15 |
| 2020-05-05 |  200 |     17 |
| 2020-05-06 |  250 |     12 |
| 2020-05-07 |  150 |     15 |
| 2020-05-08 |  300 |     12 |
| 2020-05-09 |  200 |     18 |
+------------+------+--------+

How to Get Sales Data of Current Month in MySQL

Here’s the SQL query to get records of current month in MySQL
mysql> select * from sales
       where MONTH(order_date)=MONTH(now())
       and YEAR(order_date)=YEAR(now());
+------------+------+--------+
| order_date | sale | orders |
+------------+------+--------+
| 2020-05-01 |  250 |     14 |
| 2020-05-02 |  150 |     20 |
| 2020-05-03 |  300 |     21 |
| 2020-05-04 |  200 |     15 |
| 2020-05-05 |  200 |     17 |
| 2020-05-06 |  250 |     12 |
| 2020-05-07 |  150 |     15 |
| 2020-05-08 |  300 |     12 |
| 2020-05-09 |  200 |     18 |
+------------+------+--------+

We utilize the system function now() to acquire the most recent date and time in the aforementioned query. Then, in MySQL, we obtain results for the current month by filtering out entries with the same month and year as the current time. The built-in MySQL methods MONTH() and YEAR() may be used to extract the month and year from a given date.





















Welcome all to Hancie e-learning studio
Friends,
I have brought an e-learning platform for you from where you can teach internet, website, programming language, error troubleshooting, etc. Blogger, WordPress templates, themes are available for free on this website, which are not charged for downloading, so that you can earn money by blogging using such templates and this is not a difficult task and this You can do it now. You keep uploading new posts by us and keep taking advantage of this website. The aim of which is to spread the knowledge related to internet, career, web designing and technology to the people and contribute to the development of the country.