0
+----+----------+-------------+-----------+---------------------+---------+---------------------------+
| id | action   | object_type | object_id | created_at          | user_id | object_title              |
+----+----------+-------------+-----------+---------------------+---------+---------------------------+
|  1 | shared   |           0 |         1 | 2016-04-22 06:13:16 |       1 | 5 highest buildings       |
|  2 | liked    |           0 |         1 | 2016-04-22 06:15:02 |       1 | 5 highest buildings       |
|  3 | liked    |           0 |         1 | 2016-05-21 19:02:18 |       2 | 5 highest buildings       |
|  4 | liked    |           1 |         3 | 2016-05-21 19:02:21 |       3 | 5 largest bridges         |
+----+----------+-------------+-----------+---------------------+---------+---------------------------+

I'm trying to build some thing similar to Facebook's news feed. I mean when multiple users in my friend list like same post, we will show only single result in th news feed saying that friend1, friend2, frnd3, etc., like this xyz post.

From this table I would like to group by on these three columns (action, object_type, object_id) and a list of users for this object.

SELECT *
FROM mytable
GROUP BY action, object_type, object_id

But this gave me some weird results, and I was expecting a list of users. I' really don't have any clue how to do that in this query.

Any help or lead will be appreciated.

3
  • You can try this solution for : SELECT action, object_type, object_id, count(user_id) as user FROM mytable GROUP BY action, object_type, object_id Commented Jun 30, 2016 at 9:26
  • 1
    Add a few more rows of sample data, and then show us the expected result! Commented Jun 30, 2016 at 9:28
  • use GROUP_CONCAT to get a list of grouped rows. Commented Jun 30, 2016 at 9:33

1 Answer 1

1

Try this query

SELECT action, object_title,count(user_id) as user_count, group_concat(user_id) as user_ids 
   FROM mytable GROUP BY action, object_type, object_id;
Sign up to request clarification or add additional context in comments.

4 Comments

so this would return a count of users who made any action. right? how can I get those ids instead of count.?
Thanks ... I didn't know about group_concat at all. :)
Mahesh, one more thing, user_ids are foreign key to user table, instead of user_ids, is it possible to get their emailid/username in the same query? other wise i have to make 2 queries

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.