MySQL: GROUP BY Day, Month and Year
Question by Compi | Last update on 2023-01-18 | Created on 2016-04-24
I have some data sets with a timestamp column in which the date of the record is specified. Now, I would like to determine the number of entries falling into the period of a specific year, month or day.
How can I implement that using MySQL? I only know that I probably have to use COUNT(id) and GROUP BY, but I have no idea how to write such a query.
Related Topics
MySQL: Group Timestamp Column by Month and Year
Tip | 0 Comments
JavaScript: Get current Date and Time
Tutorial | 1 Comment
PHP: Current Date and Time
Tutorial | 0 Comments
Delphi/Lazarus: Display current Date and Time
Tip | 0 Comments
Chinese Horoscope: Zodiac Signs and Elements
Info | 0 Comments
Stock Exchange: Order Validities
Info | 0 Comments
Using MySQL as a Calculator
Tip | 0 Comments
Important Note
Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.
Participate
Ask your own question or write your own article on askingbox.com. That’s how it’s done.
COUNT(id) and GROUP BY is a good choice for doing that. In the following example, the date column is "dat".
Here is how to get the number grouped by years:
Here is how to get the number grouped by months:
And here is how to get the number grouped by days:
You can read more about that in this tutorial.
Last update on 2023-01-18 | Created on 2016-04-24
After group by using month, year and day the data shows as vertical list data...
Is it possible we can transform into horizontal list data?
2017-12-20 at 10:13
The easiest way to do this, is just to use the same query but output them rotated.
For example, if using PHP, you can load the result into an array and go through this array as you want.
If you are using MySQL from shell, you can append \G to your query to rotate the result.
2017-12-20 at 13:51