View Single Post
Old 10-04-2012, 12:57 AM   PM User | #9
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,172
Thanks: 59
Thanked 3,994 Times in 3,963 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Try this...
Code:
SELECT p.postId, p.roomId, p.uniqueId, p.title, p.post, p.image, p.createdOn, r.created
FROM posts p
INNER JOIN (
   SELECT u.postid, MAX(u.createdOn) AS created
   FROM (
     SELECT p.postId, p.createdOn
     FROM posts p
     WHERE p.deleted = 0
         UNION 
     SELECT p.postId, c.createdOn 
     FROM comments c
     LEFT JOIN posts p
     ON p.postId = c.postId
     WHERE p.deleted = 0
     AND c.deleted = 0
         UNION
     SELECT p.postId, r.createdOn
     FROM replies r
     LEFT JOIN comments c
     ON c.commentId = r.commentId
     LEFT JOIN posts p
     ON p.postId = c.postId
     WHERE p.deleted = 0
     AND c.deleted = 0
     AND r.deleted = 0
   ) AS u
   GROUP BY u.postid
) AS r
ON p.postId = r.postId
WHERE p.deleted = 0
ORDER BY r.created DESC
I think that's right. Note how using MAX( ) ensures that, indeed, you will get only one "created" date/time for each postid.

If I have the sense of it wrong, you might want to change MAX( ) to MIN( ).
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
TGeene (10-09-2012)