我有一个像这样的表格sql
post_id point user_id
5 1.0 1
6 1.5 1
7 0.0 2
8 1.5 3
9 1.0 3
...我想把数据打印到这个(按user_id分组)
user_id post_id(point) total point
1 5(1.0) | 6(1.5) | ... 2.5
2 7(0.0) | | ... 0.0
3 8(1.5) | 9(1.5) | ... 3.0
...我试过这样做,但它返回了数组
SELECT * FROM table WHERE post_id = ?如果这是个愚蠢的问题,我很抱歉,但我是MySQL的新手
发布于 2019-04-11 10:59:04
运行一个查询,在其中GROUP BY user_id。使用GROUP_CONCAT()和CONCAT()生成点/帖子列。CONCAT()将字符串组合在一起形成postID (points)格式,GROUP_CONCAT()将按该组分组的所有值(在本例中为user_id)放在一个字符串中,该字符串由分隔符|分隔。
SELECT user_id,
GROUP_CONCAT(CONCAT(post_id, '(', points, ')') SEPARATOR ' | ') as point_id_posts,
SUM(points) as totalPoint
FROM table
GROUP BY user_idhttps://stackoverflow.com/questions/55630739
复制相似问题