Good day!
I have a table, let's call it conditionally "auth". Its structure is simplified to such a form that it is clearer:
id INT(11) PK AUTO_INCREMENT user_id INT(11) date DATETIME
Suppose it is recorded user authorization (user_id) in a certain system, and the date of authorization (date). One user can have multiple authorizations, one-to-many relationship.
My task is using MySQL tools to display a report on the selected time interval (from and to) of the following form:
user | 2015-01-01 | 2015-01-02 | 2015-01-03 | | | | | x | 2 | 0 | 3 | y | 0 | 1 | 1 | z | 0 | 0 | 1 |
In this example, the number of user authorizations x, y and z (user_id IN (x, y, z)) is requested, for the period from 2015-01-01 to 2015-01-03 (date> = '2015-01-01 00:00:00 'AND date <=' 2015-01-03 23:59:59 ')
How can I derive a multidimensional result and place the report dates “horizontally”, while substituting the missing dates (or at least without them)?
This is how I can display the number of authorizations, but only for one user (x):
SELECT COUNT(*) AS CNT, DATE_FORMAT(`date`, '%Y-%m-%d') as DATE FROM auth WHERE user_id = x GROUP BY DATE ORDER BY DATE DESC;
But how to do this for multiple users, as in the example? Really for each user it is necessary to make nested select on the number of authorizations by dates?
Thank!
user_id
. less obvious - adduser_id
to the grouping list. approximately:select ... from auth group by user_id, date order by ...
- aleksandr barakin