Skip to content

Instantly share code, notes, and snippets.

@mikebuchanon
Last active December 14, 2015 15:59
Show Gist options
  • Save mikebuchanon/5112347 to your computer and use it in GitHub Desktop.
Save mikebuchanon/5112347 to your computer and use it in GitHub Desktop.
find count of read and unread posts given userid and courseid
SELECT d.id AS discussion,
f.name AS name,
sum(case when r.postid is null then 1 else 0 end) as unread_posts,
sum(case when r.postid is null then 0 else 1 end) as read_posts
FROM mdl_course c
JOIN mdl_forum_discussions d on d.course = c.id
JOIN mdl_forum f on f.id = d.forum
JOIN mdl_forum_posts p ON p.discussion = d.id
LEFT JOIN mdl_forum_read r ON r.postid = p.id
WHERE c.id = 3
and r.userid=4
group by d.id, f.name;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment