如何统计不同表中的内容并按用户分组?

问题描述 投票:0回答:1

我有那3张桌子 enter image description here 我不知道如何将它们连接起来以获取每个用户的 users.id、用户名、点赞数和评论数。我想把它分成 4 列。

我尝试过这个: 选择 u.id、u.username、count(l.photo_id) 作为 posts_liked,从用户中添加 u.id = l.user_id group by u.id 联合所有 select u.id, u.username, count(c.user_id) as com_writing from users u left join comments c on u.id = c.user_id group by u.id; 但结果只是 3 个表,其中一列中有评论数和点赞数。

group-by count sum union
1个回答
0
投票

我将三个表连接起来,然后按id分组。如果它能回答您的问题,请尝试一下

© www.soinside.com 2019 - 2024. All rights reserved.