views:

58

answers:

0

I have a simple flat comment system and I would like to expand it to allow one level of replies to the root level comments. I would also like to be able to have the results sorted by the root level comment in ASC or DESC order or some kind of rank but the child replies sorted always by newsest first and then still be able to select results in one pass with a LIMIT clause.

This is what I have so far...it semi works but there has to be a better solution?

SELECT c1.*
     , if(  c1.parent_id is null
          , c1.comment_id
          , c2.comment_id
         ) AS sort_id
  FROM `comment` c1
LEFT JOIN `comment` c2 ON (c1.parent_id=c2.comment_id)
ORDER BY sort_id, date;