E
E
evilelf2015-09-05 13:26:26
MySQL
evilelf, 2015-09-05 13:26:26

How to group results by day in SQL query?

Hello.
There is a table, in it a field with date and time Ymd H:i:s
Task: it is necessary to return the grouped data of a field with date on days|hours|months.
What is the best way to do this?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
H
heartdevil, 2015-09-05
@heartdevil

Hello.
The request will be something like this, if I understand correctly.

SELECT t.field1, t.field2 FROM table AS t
WHERE condition
GROUP BY DAYOFMONTH(t.date), HOUR(t.date), MONTH(t.date)

D
Dmitry, 2015-09-05
@thewind

DATE_FORMAT + GROUP BY

N
Nikolai Matyushenkov, 2015-09-10
@mnv

Hourly:
SELECT ... FROM table AS t WHERE ...
GROUP BY DATE(t.date), HOUR(t.date) Daily
:
SELECT ... FROM table AS t WHERE ...
GROUP BY DATE(t .date)
By month:
SELECT ... FROM table AS t WHERE ...
GROUP BY YEAR(t.date), MONTH(t.date)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question