Jump to content

Sort topics by most recent comment


mikwit

Recommended Posts

okay so I have two tables, one table has the topics, while the other has all the comments for all the topics.

main                                     comments                
________________               __________________
|number|text|date|             |reply to|time|date|
|______|____|____|             |__________________|

Currently, when I query for topics, I get number, text, time + count of comments whose replyto = number through this query.

SELECT `number` , `date` , `text`, b.CommentCount 
	FROM main a
	LEFT OUTER JOIN (
	SELECT replyto, COUNT( `text` ) AS CommentCount
	FROM `comments`
	GROUP BY replyto
	)b ON `number` = b.replyto
	ORDER BY `date` DESC LIMIT '.$lowlim.', 15

Instead of ORDER BY date (of the post), I would like to figure out/find a way to order by most recent date of comment.  Any help or guidance would be greatly appreciated

Link to comment
Share on other sites

  • 2 weeks later...

So I've gotten it to work sort of

SELECT `number` , `school` , `date` , `text` , `category` , `likes` , `dislikes` , `md5` , `banned` , b.CommentCount , `name` , `title`, m.last_date 
	FROM main a
	LEFT OUTER JOIN (
	SELECT replyto, COUNT( `text` ) AS CommentCount
	FROM `comments`
	GROUP BY reply_to
	)
    b ON `number` = b.reply_to
	left outer JOIN ( 
    select reply_to, max(`date`) AS last_date 
    from `comments`
	GROUP BY reply_to)
    m on `number` = m.reply_to
    NATURAL JOIN college_list
	WHERE school = 21
	ORDER BY last_date 

This works, but I get null fields and it messes with the sorting... I've tried a max(ifnull(`date`,[date from post] )) but I don't know how to call the date of the post.  So is there there a way to do Order by maximum(last_date,date) ??

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.