Skip to content

Instantly share code, notes, and snippets.

@tburry
Last active June 1, 2016 15:26
Show Gist options
  • Save tburry/39a5544a69a8b19e4ff99f396cc32aec to your computer and use it in GitHub Desktop.
Save tburry/39a5544a69a8b19e4ff99f396cc32aec to your computer and use it in GitHub Desktop.
Update database counts in Vanilla.
-- Count discussions.
update GDN_Category c
set CountDiscussions = (select coalesce(count(d.DiscussionID), 0) from GDN_Discussion d where c.CategoryID = d.CategoryID);
-- Count comments.
update GDN_Discussion d
set CountComments = (select coalesce(count(c.CommentID), 0) from GDN_Comment c where c.DiscussionID = d.DiscussionID);
update GDN_Category c
set CountComments = (select coalesce(sum(d.CountComments), 0) from GDN_Discussion d where c.CategoryID = d.CategoryID);
-- Update date last comment and last post.
update GDN_Discussion d
set DateLastComment = (select coalesce(max(c.DateInserted), d.DateInserted) from GDN_Comment c where c.DiscussionID = d.DiscussionID);
update GDN_Discussion d
join GDN_Comment c
on c.DiscussionID = d.DiscussionID and d.DateLastComment = c.DateInserted
set LastCommentID = c.CommentID;
update GDN_Category cat
set LastDateInserted = (select max(d.DateLastComment) from GDN_Discussion d where d.CategoryID = cat.CategoryID);
update GDN_Category cat
join GDN_Discussion d
on d.CategoryID = cat.CategoryID and d.DateLastComment = cat.`LastDateInserted`
set cat.LastCommentID = d.LastCommentID,
cat.LastDiscussionID = d.DiscussionID;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment