如何按用户组筛选主题

如果您的论坛安装了数据探索器插件(Data Explorer Plugin),您就可以编写 SQL 查询语句从 Discourse 数据库中获取这些信息。此查询尝试获取主题的 ID 和标题,以及主题中最后一篇帖子的用户 ID。它只选择最后一篇帖子是由“clients”组中的用户发布的那些主题。

这只是一个示例。

SELECT 
    t.id AS topic_id, 
    t.title AS topic_title, 
    (SELECT user_id FROM posts WHERE topic_id = t.id ORDER BY created_at DESC LIMIT 1) AS last_post_user_id
FROM 
    topics t
JOIN
    posts p ON p.topic_id = t.id
JOIN 
    group_users gu ON gu.user_id = p.user_id
JOIN 
    groups g ON g.id = gu.group_id
WHERE 
    g.name = 'clients'
GROUP BY 
    t.id
HAVING 
    MAX(p.user_id) = last_post_user_id
ORDER BY 
    MAX(p.created_at) DESC
1 个赞