- Instant help with your Sql coding problems

Group by year of Unix timestamp in MySQL

Question:
How to group by year of Unix timestamp in MySQL?
Answer:
SELECT FROM_UNIXTIME(event_time, '%Y') AS year, COUNT(id) AS total 
FROM events
GROUP BY FROM_UNIXTIME(event_time, '%Y') DESC;
Description:

If we want to group records by date in a MySQL table where the value is stored as a Unix timestamp, we can use FROM_UNIXTIME() function in the GROUP BY clause.

If we want to group by date only (without time), we can use the corresponding date formatting string as the second parameter.

Share "How to group by year of Unix timestamp in MySQL?"