SQL列在多列上 - MySQL

时间:2016-06-30 09:25:08

标签: mysql sql

+----+----------+-------------+-----------+---------------------+---------+---------------------------+
| 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         |
+----+----------+-------------+-----------+---------------------+---------+---------------------------+

我试图建立一些类似于Facebook新闻源的东西。我的意思是当我朋友列表中的多个用户喜欢同一个帖子时,我们只会在新闻Feed中显示单个结果,说明friend1,friend2,frnd3等,就像这个xyz帖子一样。

在此表格中,我想分组这三列(actionobject_typeobject_id)以及此对象的用户列表。

SELECT *
FROM mytable
GROUP BY action, object_type, object_id

但这给了我一些奇怪的结果,我期待着一个用户列表。我'真的没有任何线索如何在这个查询中做到这一点。

任何帮助或领导都将不胜感激。

1 个答案:

答案 0 :(得分:1)

尝试此查询

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;