I have some related tables
USERS
+----+--------------------------+------------+
| id | email | first_name |
+----+--------------------------+------------+
| 2 | [email protected] | Loren |
| 3 | [email protected] | Fernando |
| 4 | [email protected] | Evelyn |
+----+--------------------------+------------+
EVENTS
+----+------------+
| id | name |
+----+------------+
| 1 | Dolores ve |
| 2 | Harum erro |
| 3 | Ratione qu |
+----+------------+
EVENT_USER (join)
+---------+----------+
| user_id | event_id |
+---------+----------+
| 1 | 3 |
| 1 | 13 |
| 1 | 48 |
+---------+----------+
along with other tables related to user ( messages, connections ) etc
I want to get a count of messages, connections and other related data for users of a specific event
SQL I've tried:
SELECT u.`id`, u.`first_name`, u.`last_name`,
COUNT(m.`id`) as message_count,
COUNT(a.`id`) as article_count
FROM `users` u
LEFT JOIN `event_user` eu ON u.`id` = eu.`user_id`
LEFT JOIN `messages` m ON u.`id` = m.`from`
LEFT JOIN `articles` a ON u.`id` = a.`user_id`
WHERE
eu.`event_id` = 3
GROUP BY u.`id`
But the counts are coming out to be same for both columns.
i.e. message_count and article_count are same for all users.