I have two databases, each containing one table, users.user_groups and logins.logins.
The first one contains a list of users and their corresponding groups as follows :-
mysql>select * from users.user_groups;
+----------+-------------------+
| username | group_type |
+----------+-------------------+
| user_1 | group_1 |
| user_2 | group_1 |
| user_3 | group_1 |
| user_4 | group_2 |
| user_5 | group_2 |
| user_6 | group_2 |
| user_7 | group_3 |
| user_8 | group_3 |
| user_9 | group_3 |
+----------+---------+
so user_1, user_2, user_3 form part of group_1, user_4, user_5, user_6 form part of group_2 etc.
Table logins, inside database logins contains a track record of when the users log in the system :-
select * from logins.logins;
+-----------+---------------------+
| user_name | login_date |
+-----------+---------------------+
| user_1 | 2014-06-01 09:11:09 |
| user_1 | 2014-06-02 02:11:09 |
| user_2 | 2014-06-08 02:13:43 |
| user_1 | 2014-06-02 03:13:42 |
| user_3 | 2014-06-02 03:13:42 |
| user_2 | 2014-06-02 03:13:42 |
| user_1 | 2014-06-08 03:13:42 |
| user_4 | 2014-06-02 03:13:42 |
| user_5 | 2014-06-02 03:13:42 |
| user_8 | 2014-06-02 03:13:42 |
| user_9 | 2014-06-02 03:13:42 |
+-----------+---------------------+
11 rows in set (0.00 sec)
I need to gather statistics of how the groups are logging in the system, and I managed this so far :-
SELECT user_name, group_type, COUNT(*) as number_of_logins
FROM logins.logins, users.user_groups
WHERE username = user_name and group_type = 'group_2'
GROUP BY user_name
ORDER BY group_type, user_name;
which gives me
+-----------+------------+------------------+
| user_name | group_type | number_of_logins |
+-----------+------------+------------------+
| user_4 | group_2 | 1 |
| user_5 | group_2 | 1 |
+-----------+------------+------------------+
2 rows in set (0.00 sec)
But I also need it to tell me user_6 has 0 number of logins like this :-
+-----------+------------+------------------+
| user_name | group_type | number_of_logins |
+-----------+------------+------------------+
| user_4 | group_2 | 1 |
| user_5 | group_2 | 1 |
| user_6 | group_2 | 0 |
+-----------+------------+------------------+
I would be grateful if anyone would point me in the right direction
I have managed a solution via php using for / next loops, but I believe it would be faster via straight sql
Thanks in advance, and apologies for the lengthy question
J