Skip to content

Instantly share code, notes, and snippets.

@mrpunkin
Created September 7, 2012 18:42
Show Gist options
  • Save mrpunkin/3668488 to your computer and use it in GitHub Desktop.
Save mrpunkin/3668488 to your computer and use it in GitHub Desktop.
SELECT groups.id, groups.created_at AS group_created, MAX(gu.created_at) AS newest_user, MAX(gp.created_at) AS newest_photo, GREATEST(groups.created_at, IFNULL(MAX(gu.created_at), 0), IFNULL(MAX(gp.created_at), 0)) AS latest_activity FROM groups
LEFT OUTER JOIN groups_users gu ON groups.id = gu.group_id
LEFT OUTER JOIN group_photos gp ON groups.id = gp.group_id
GROUP BY groups.id
id group_created newest_user newest_photo latest_activity
1 2009-12-01 18:39:25 2010-07-20 21:23:50 NULL 2010-07-20 21:23:50
2 2010-06-14 15:28:21 2010-06-14 15:34:01 NULL 2010-06-14 15:34:01
8 2010-07-15 16:14:47 2011-06-23 18:50:52 2012-09-05 17:03:09 2012-09-05 17:03:09
9 2012-09-05 18:48:07 NULL NULL 2012-09-05 18:48:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment