我有一个包含以下行的表格:
id. user_id, type - link
1. 555, image - http://1
2. 555, image - http://2
3. 654, image - http://3
4. 245, video - http://..
5. 555, image - http://..
6. 878, text - http://..
我想按日期对类型(图像)进行分组,因此它们显示为单行。在此示例中,前两个图像将合并在一起,输出如下所示,还要注意,如果不是同一用户,则不会为该用户分组。
输出
1. 555, image - http://1, http://2 ** GROUPED BY DATE, if they are same type and not break type after it.
2. 654, image - http://3
3. 245, video - http://..
4. 555, image - http://..
5. 878, text - http://.
顺便说一下,我想把 facebook 做成 news feed,如果有人有更好的想法,请分享。
最佳答案
SELECT `date`, `user_id`, `type`, GROUP_CONCAT(`link`)
FROM `table`
GROUP BY `date`, `user_id`, `type`
关于sql - mysql查询复杂分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3412924/