我有一张桌子tasks,看起来像这样:
userId caption status id
1 Paul done 1
2 Ali notDone 18
3 Kevin notDone 12
3 Elisa notDone 13我将它与另一个表users连接起来,以查找status = notDone所在的tasks的数量。我这样做:
SELECT u.id,
t.number_of_tasks,
FROM users u
INNER JOIN (
SELECT userId, COUNT(*) number_of_tasks
FROM tasks
WHERE status = "notDone"
GROUP BY userId
) t ON u.id = t.userId
"""现在,我希望创建另一列captions,该列以某种方式包含了包含在count中的所有captions的列表,并满足join + where条件。
例如,我希望这是行之一。我怎样才能做到这一点?
userId number_of_tasks captions
3 2 ["Kevin", "Elisa"]发布于 2022-04-17 19:24:15
您可以在子查询中使用json_group_array()聚合函数为每个用户创建标题列表:
SELECT u.id, t.number_of_tasks, t.captions
FROM users u
INNER JOIN (
SELECT userId,
COUNT(*) number_of_tasks,
json_group_array(caption) captions
FROM tasks
WHERE status = 'notDone'
GROUP BY userId
) t ON u.id = t.userId;https://stackoverflow.com/questions/71904605
复制相似问题